This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to set form field values using javascript? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "How to set form field values using javascript?" Watch "How to set form field values using javascript?" New topic
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
    
    6
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
    
  13

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
    
  13

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
    
  13

Show us your new code. Please be sure to use UBB code tags when posting.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to set form field values using javascript?
 
Similar Threads
web application in eclipse 9
does firefox have a problem with .setAttribute ?
Requested Resource Not Found Error
javascript error: object expected
why document.getElementById work with form elements