1

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>
Jiawei Ma
  • 11
  • 3
  • Elements with `id` are part of `document` object and can be accessed as `document.id` but its not the idea way. – Rajesh Dec 07 '16 at 08:09
  • Think about, what will happen if your html contains multiple form? Then how can you access a particular form by using `document.form`? – Ataur Rahman Munna Dec 07 '16 at 08:09
  • @Ataur Rahman Munna: By the forms name. document.myform1; document.myform2; .. – Lain Dec 07 '16 at 08:12
  • "the right usage of DOM to get a element is using getElementById or getElementsByTagName" I guess you forgot (and should check it out if you didn't yet) document.querySelector method. To be honest after I've learned JS and started messing around with it, using in some projects, this is the #1 method that I use for querying the DOM. document.myform is there in that simplified version do you do not need to use that getElementByIDS etc. It is because it is unusual to have many forms (> 1) on one page (however still - it can happen). Usually there is just one form on one page. – MindRoller Dec 07 '16 at 08:28
  • 1
    http://stackoverflow.com/questions/2435525/best-practice-access-form-elements-by-html-id-or-name-attribute – Lain Dec 07 '16 at 08:31

0 Answers0