Forum Moderators: open

Message Too Old, No Replies

document.form1 & document.form1.end is undefined

makes no sense to me

         

kingoslo

12:28 am on Feb 20, 2010 (gmt 0)

10+ Year Member



Hello there,

I have this document. It's been shortened to not make the code long and annoying to read. I didnt know what was causing the problem, and didnt want to get rid of too much.

I get element undefined in Chrome and firefox with this part of the code:

document.form1.end.value = Time;


Also the watch-script (one of the javascripts) will not run without an additional (as you can see in the document underneath):
 function display() {
setTimeout("display()",1000);
}

display();


This is the document:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">

function display() {
setTimeout("display()",1000);
}

display();

</script>

<script src="jquery-1.4.1.js" type="text/javascript"></script>

<script language=JavaScript>
$(document).ready(function() {
$('#reset').click(function() {
event.preventDefault();
$.post('ajax_null.php', function() {
window.location = "/graf.php?refresh=true&";
});
});
});

</script>

<script type="text/javascript" language="javascript">
function display() {
var Today = new Date();
var hours = Today.getHours();
var mins = Today.getMinutes();
var sec = Today.getSeconds();
var year = Today.getFullYear();
var day = Today.getDate();
var month = Today.getMonth() + 1;
var Time = ((hours < 10) ? '0' + hours : hours);
Time += ((mins < 10) ? ":0" : ":") + mins;
Time += ((sec < 10) ? ":0" : ":") + sec;
Time += ' ';
Time += ((month < 10) ? '0' : '') + month + '/';
Time += day + '/';
Time += year;
document.form1.end.value = Time;
setTimeout("display()",1000);
}

display();
</script>


</head>

<body>

<div>
<span style="display:inline-block; vertical-align:top;">
<strong>Tegn graf for:</strong>
</span>
<span style="display:inline-block; vertical-align:top;">
<img src="img_graphical.php" />
<form name="form1" action="/graf.php" method="post">
<input name="start" value="00:48:21 02/18/2010" />
<input type="text" name="end" value="00:48:21 02/20/2010" />
<input name="iterations" value="1800" style="width:55px;" />
<input type="submit" value="Oppdater" /><input type="submit" id="reset" value="Tilbakestil" />
</form>
</span>
<span style="display:inline-block; vertical-align:top;">
&nbsp;
</span>
</div>
</body>
</html>


I've worked at this for hours, and I have no idea what is causing these problems.

Thank you for your time.

Kind regards,
Marius

Fotiman

1:06 am on Feb 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try document.forms["form1"] instead of document.form1.

kingoslo

1:11 am on Feb 20, 2010 (gmt 0)

10+ Year Member



Dear Moderator Fotiman,

Thank you for your reply and time.

I replaced it with the following code, and it did not make a difference:

document.forms["form1"].end.value = Time;


What are your thoughts?

Thank you.

Kind regards,
Marius

kingoslo

1:45 am on Feb 20, 2010 (gmt 0)

10+ Year Member



Also, I get event not defined here:

event.preventDefault();


Thanks,
Marius

rainborick

2:35 am on Feb 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It might be because you're executing the JavaScript before the form has been rendered/created by the browser, so it doesn't exist yet. If you aren't doing so already, try calling the function in an onload() attribute on the <body> tag.

kingoslo

5:33 pm on Feb 20, 2010 (gmt 0)

10+ Year Member



rainborick: You are expert. You solved my problem.

Thanks for your time,
Marius