PDA

View Full Version : Simple JavaScript problem


enuenu
10-13-2007, 10:22 AM
Can anyone tell me why the code below does not work. When the user clicks anywhere on the form the current date is supposed to be displayed in the input box. It does not work and I cannot work out why.

<html>
<title>Date form</title>
<script type="text/javascript" language="JavaScript">

/*function to get today's date */
function today(){var theDate = new Date();
return theDate;}

/*function to get date to load on form click */
function start(){document.getElementById.('formdate').value=today();}

</script>
</head>
<body>
<form name="date" onclick="start()">
Date: <input id="formdate" name="formdate" size="50" />
</form>
</body>
</html>

BonRouge
10-13-2007, 10:45 AM
Remove this dot:function start(){document.getElementById.('formdate').value=today();}

coothead
10-13-2007, 10:50 AM
Hi there enuenu,

try it like this...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Date form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
window.onload=function(){
document.forms[0].onclick=function(){
start();
}
}
function start(){
document.forms[0][0].value=new Date();
}
</script>

</head>
<body>

<form action="#">

<div>
<label>Date: </label>
<input name="formdate" size="55" >
</div>

</form>

</body>
</html>

enuenu
10-13-2007, 10:59 AM
Thanks BonRouge, you saved my sanity. Thanks also coothead, I will explore your solution.