Topic: How to change value of text box using javascript with button

Hi,
I have one date field which is coming up with the previous date entered. I want to add one button beside it and on clicking to that button that date field should be cleared.

I have written my code like this:

<%= if @claim.nil? then date_field('claim', 'hrh_diary_date') else date_field('claim', 'hrh_diary_date', :value=>@claim.hrh_diary_date) end %>
      <% if @date %>
        <br /><b><i>(Your current diary date is <%= @date.to_date if !@date.nil? %>)</i></b>
          <input type="button" value="clear diary date" onclick="clear_diary_date(this);">
      <% end %>

And my java script is:

function clear_diary_date()
{
    document.getElementById('hrh_diary_date').value = '';
}

Can anyone please tell me what is wrong with the code? Or is there any other way to do this

Thanks in advance

Re: How to change value of text box using javascript with button

It may be that your passing a value to the clear_diary_date method "(this)", but in your function definition it doesn't take any variables.

Alex

Re: How to change value of text box using javascript with button

Thanks Alex for ur response but I still have problem,

I tried with clear_diary_date method() also but actually I am getting javascript error as 'hrh_diary_date' Undefined.


document.getElementById('hrh_diary_date').value = ''; ---- is this right or I have to use something else to get the value of hrh_diary_date so that I can set it at blank.

Thanks
Atit

Last edited by atit.shah (2007-07-25 17:19:42)

Re: How to change value of text box using javascript with button

No, that looks OK. Have you checked the source to make sure that your field actually has the attribute id = 'hrh_diary_date'?

Alex

Re: How to change value of text box using javascript with button

Hey Alex,

Thanks for your help. I got the solution.

I use this thing
<%= button_to_function "Clear Diary Date" do |page|
          page[:claim_hrh_diary_date].value = ''
          end %>

Atit