PDA

View Full Version : Addressing a text box from javascript



ScottInTexas
07-16-2004, 07:04 PM
OK, I'm obviously missing something basic about addressing elements on my webpage because I have this problem all the time.

Here is the area of the web page in which the text box resides.


<div id="updateDateDiv">
<input type="text" value="" size="10" id="upDateDate" />
<input type="button" value="Update" onClick="SetUpdateDate()" />
Update Date
</div>


and here is the javascript;


function SetUpdateDate(){
var todaysDate = new Date();
var txtDate = monthName[todaysDate.getMonth()] + " " + todaysDate.getDay() + ", " + todaysDate.getYear();
document.upDateDate.value=txtDate;
}


This generates an error "upDateDate is null or not an object.

Thanks for your help. I know I'll feel stupid when I see the answer.

Frank
07-16-2004, 07:28 PM
I would suggest you use a form tag and change the "id" attribute to a "name" attribute , and include the form name in the javascript.


So your htm would look like this.


<html>
<head>
<script type=" type="text/javascript"">
function SetUpdateDate(){
var todaysDate = new Date();
var txtDate = monthName[todaysDate.getMonth()] + " " + todaysDate.getDay() + ", " + todaysDate.getYear();
document.form1.upDateDate.value=txtDate;
}
</script>
<head>
<body>
<form name="form1">


<div id="updateDateDiv">
<input type="text" value="" size="10" name="upDateDate" />
<input type="button" value="Update" onClick="SetUpdateDate()" />
Update Date
</div>

</form>
</body>
</html>


There maybe a better way to do this but this is how I do it.

Basscyst
07-16-2004, 10:32 PM
Hello,

Being it is just a snippit of code I would assume you have a form tag. In which case the proper syntax would be:

document.forms[x].elements[x].value (x being the form or element number with in the document or form respectivly)

or

document.formName.FieldName.value

or

document.getElementById('fieldName').value

Basscyst