I have the following JavaScript in my HTML page referencing an HTML form on the page:
<script type="text/javascript">
<!--
var myForm = document.myForm;
function validateForm() {
if (myForm.myInput == "")
alert("Please input some text.");
return false;
}
myForm.submit();
}
function showFormInput() {
myForm.reset();
document.getElementById('myInput').style.display = 'inline';
}
//-->
</script>
...
<form name="myForm" id="myForm" action="..." method="post">
<input id="myInput" name="myInput" type="text" value="" style="display:none;" />
</form>
Both functions are throwing an exception when trying to access the variable myForm, saying that “myForm is null or not an object”. Why is this occurring?
UPDATE: One thing I think I’m gathering from this is that global variables should generally be used for string literals – not elements in the DOM. I”m going to go forward with this, and use element variables sparingly, and only after the DOM has been loaded.
What browser are you using?
IN general you should never use
document.*access. This is an MS-IE convention. Instead, useAs outis noted, either put your script block at the bottom of your page, or use the
onloadevent of <body> tag, like this