| Author |
How to set form field values using javascript?
|
rick collette
Ranch Hand
Joined: Mar 22, 2002
Posts: 208
|
|
Hi, The following code is on my second page. There is a form field called time, I want to set it to user's current time after user hits submit button on the first page and comes to this page. I use javascript code like the following to do that. But it seems it never sets the time value after I come to second page: <script language="JavaScript"> times=new Date(); document.form2.time.value = times; </script> <form name="form2" action="nextPage.php" method="post"> <table> <tr> <td> Name:<input id="name" size="10" name="name"></td> <td> Phone:<input id="phone" size="10" name="phone"></td> <td> Zip Code:<input id="zipcode" size="5" name="zipcode"></td> <td> Address:<input id="address" size="30" name="address"></td> <td> Time:<td><input name="time" value="" readonly /></td> </tr> <tr> <td><input type="button" value="Submit" name="Submit2"></td> </tr> </table> I am wondering what I did wrong here? thanks,
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
You are setting the field before it is even exists on the page. It is like going to a restuarant and they call your name before you even leave to go there. You need to set it onload or after the control os rendered. Eric
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
Firstly, since you are doing nothing with request params on this page, the fact that it is a "second page" is moot. But take a look at your markup. Where is the reference to the input field? Now where is the input field defined? Anything clicking?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
Eric beat me to the punch... You would also be well-served to debug your code in Firefox where the Javascript console would have let you know that there was a problem.
|
 |
rick collette
Ranch Hand
Joined: Mar 22, 2002
Posts: 208
|
|
Thanks for giving me pointers. Unfortunately, even after I set onload event on the body tag, it still does not work. I guess something must be wrong somewhere else.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56192
|
|
|
Show us your new code. Please be sure to use UBB code tags when posting.
|
 |
 |
|
|
subject: How to set form field values using javascript?
|
|
|