Click to See Complete Forum and Search --> : javacript and form elements


bobo
February 16th, 2006, 05:11 PM
I cant believe I cant get this going... I simply want to impliment a value into a text field using javascript. whats wrong with this. The error I get says "document.con.comments" is null or does not exist. :blush:

(I simplified dramaticaly here for time purposes)

<html>
<head>
<title>Untitled</title>

<script language="javascript">

var fieldtext = "hello world";
document.con.comments.value = fieldtext;

</script>

</head>
<body>

<form action="" name="con" id="con">
<input type="text" name="comments">
</form>

</body>
</html>

bobo
February 16th, 2006, 05:18 PM
forget it.... because it out of scope... works when the code is at the bottom of the page not in the header.

Dr. Script
February 16th, 2006, 06:03 PM
Simply put, you are getting the error because you are trying to change an object that doesn't exist yet. Moving it to the bottom, or moving it into the onload event handler solves the problem.

PeejAvery
February 16th, 2006, 09:22 PM
forget it.... because it out of scope... works when the code is at the bottom of the page not in the header.
Don't forget it. This is easily solved. I will do exactly what Dr. Script advised.

<html>
<head>
<title>Test Page</title>
<script language="javascript">
function changetext(){
var fieldtext = "hello world";
document.con.comments.value = fieldtext;
}
</script>
</head>
<body onload="changetext()">
<form name="con">
<input type="text" name="comments">
</form>
</body>
</html>