I'm really curious In the code bellow why I can access the element from DOM by simply type document.myform, (it works in console too)and this only happens when the name attribution is in a form tag.
I assume the normal usage of DOM to get a element is using getElementById or getElementsByTagName or when accessing the form we can use document.forms(or querySelector thank @MindRoller for reminding). But how to explain the above usage. Is it explained by any documents?
The html code is as follow.
<HTML><HEAD>
<TITLE>Set Cookies</TITLE>
<SCRIPT language=JavaScript>
function saveCookies()
{
// Remove any previous cookies
//To delete a cookie, just set the expires parameter to a passed date:
document.cookie = "personColor=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
document.cookie = "personName=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
var hisName = document.myform.yourname.value;
var hisColor;
if (document.myform.backg[0].checked)
{
hisColor=document.myform.backg[0].value;
}
else if (document.myform.backg[1].checked)
{
hisColor=document.myform.backg[1].value;
}
setCookie('personName', hisName, 30);
setCookie('personColor', hisColor, 30);
alert("personName =" + hisName + "\npersonColor = " + hisColor);
window.location="js_21_5.html";
}
</SCRIPT>
</HEAD>
<BODY>
<h2>Full Cookie Example</h2>
<FORM method="GET" name="myform">
What is your name: <input name="yourname" type="text" /><BR>
Choose your background:<br>
<input type=radio name="backg" value="green">green<br>
<input type=radio name="backg" value="red">red<br>
<input type="button" value="submit" onclick="saveCookies()">
</FORM>
<a href="http://cs-server.usc.edu:45678/examples.html#cookies">
Return to main page</a>
<noscript></BODY>
</HTML>