In the following code, this line is a bit strange to me:
var x=document.forms["myForm"]["fname"].value;
The web page:
<html>
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
How does that work? Where is the multidimensional array ? thanks
This:
is exactly the same as:
In fact there’s really no reason (in this case) for it to be written the way it is.
Now, if instead of those two string constants — “myForm” and “fname” — there were some dynamic mechanism that computed or fetched the names, then the first form makes sense. The
[ ]operator allows for an expression to be evaluated to determine a property name to access.There are no arrays involved in this example at all, by the way. Just object property references.