Why are my JavaScript variables not persisting across functions?
- by Eric Belair
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?