Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Date in document form problem. 2

Status
Not open for further replies.

ZekeO238

Programmer
Oct 1, 2002
32
US
I have a form for users to fill out for my school project. In this form I have a date box so that I know the exact date that someone sent the form. I want Javascript to automatically fill in the box but it says that the its null or not an object. Heres the code:

<script language=&quot;Javascript&quot;>
<!--
var date=new Date()
var month=date.getMonth()+1
if (month<10) {month=&quot;0&quot;+month}
var day=date.getDate()
if (day<10) {day=&quot;0&quot;+day}
var year=date.getYear()
if (year<200) year+1900
var dmonth=month
var dday=day
var dyear=year
document.email.date.value=dmonth+&quot;/&quot;+dday+&quot;/&quot;+dyear
-->
</script>

Thats in the head. I then have the form here:

<form name=&quot;email&quot; method=&quot;post&quot; action=&quot;mailto:Zeke@liquid2k.com?subject=HTML Beginners Guide&quot;>
Please enter your name:<br>
<input type=&quot;text&quot; name=&quot;name&quot; size=&quot;30&quot;><br><br>
Todays Date:<br>
<input type=&quot;text&quot; name=&quot;date&quot; size=&quot;10&quot;><br><br>
Questions/Comments:<br>
<textarea name=&quot;qs_comment&quot; rows=10 cols=120 wrap=&quot;virtual&quot;>
</textarea><br><br>
Has this site been helpful?<br>
<input type=&quot;radio&quot; class=&quot;selects&quot; name=&quot;helpful_&quot; value=&quot;yes&quot; checked> Yes<br>
<input type=&quot;radio&quot; class=&quot;selects&quot; name=&quot;helpful_&quot; value=&quot;no&quot;> No<br><br>
How do you like this website?<br>
<input type=&quot;radio&quot; class=&quot;selects&quot; name=&quot;the_site_is_&quot; value=&quot;very_nice&quot; checked> Its Very Nice!<br>
<input type=&quot;radio&quot; class=&quot;selects&quot; name=&quot;the_site_is_&quot; value=&quot;ok&quot;> Its Ok.<br>
<input type=&quot;radio&quot; class=&quot;selects&quot; name=&quot;the_site_is_&quot; value=&quot;bad&quot;> Bad. Needs Improvement.<br><br>
<input type=&quot;submit&quot; value=&quot;Send&quot;>
<input type=&quot;reset&quot; value=&quot;Reset&quot;></form>

I dont understand why it says that document.email.date is null or not an object when it is right there. Any help is appreciated.

Also, can someone tell me the property I add to the form tag to make the text that is emailed to me regular text. I forget what it is.
 
You haven't loaded the page - the form doesn't exist when you are trying to set the value for &quot;date&quot;. Execute your code onLoad and the problem should disappear...
 
Move your script to some function:

function addDate() {
var date=new Date()
var month=date.getMonth()+1
. . .
document.email.date.value=dmonth+&quot;/&quot;+dday+&quot;/&quot;+dyear
}

And call on document load:
<body onload=&quot;addDate()&quot;>

The reason why you get that error is that script is executed while document is parsed by browser, i.e. before the form objects are created.
Adding a function makes sure that the scrpt will execute when the page is completely ready, and all necessary objects already exist.
 
Duh! I feel kinda stupid now. Thanks so much, I probably would have never noticed that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top