What does the document ["Some Name"] really do?
This code
<img name="n1" src="" /> <h1 name="n2">a header</h1> <script> document["n1"].src = "http://xy/picture.jpg"; document["n2"].innerHTML = "Boo"; </script> Something else for the <img> and <h1> tags. The src image changes, as expected, to the string document["n1"].src . But the innerHTML header does not change as expected in the line document["n2"].innerHTML . What does document["some string"] really return?
In the JavaScript object["string"] select the 'string' property on the object . This can be used to access many different properties for many different objects and is similar to treating an object as a hash map of values. For the document object, certain properties will be loaded by default, for example, elements with the name attribute. At least for some browsers (I donβt know which subset).
However, the name attribute is not a valid attribute of the <h1> , so the document does not load this into the value of document["name"] .
The name attribute is valid for the following elements:
<a>- Attribute deprecated in HTML 4, deprecated in HTML5<applet>- Element deprecated in HTML5<button><form>- Attribute deprecated in HTML 4 returned in HTML5<frame>- Element deprecated in HTML5<iframe><img>- Attribute deprecated in HTML 4, deprecated in HTML5<input><map><meta>- Not the samenameattribute<object><param>- Not the samenameattribute<select><textarea>
For each of these elements, if they have a name attribute, they will be added to the document, as you can see. However, the preferred way to get these elements is using document.getElementsByName()