04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: The Document Object Model<br />

This element can be retrieved using the following code:<br />

var div = document.getElementById(“myDiv”); //retrieve reference to the < div ><br />

The following code, however, would return null in all browsers except IE:<br />

var div = document.getElementById(“mydiv”);<br />

//won’t work (except in IE)<br />

IE prior to version 8 considered IDs to be case - insensitive, so “ myDiv ” and “ mydiv ” are considered to be<br />

the same element ID. This also occurs in IE 8 running in IE 7 – compatibility mode (where document.<br />

documentMode is 7).<br />

If there is more than one element with the same ID in a page, getElementById() returns the element<br />

that appears first in the document. IE 7 and earlier add an interesting quirk to this, also returning form<br />

elements ( < input > , < textarea > , < button > , and < select > ) that have a name attribute matching the<br />

given ID. If one of these form elements has a name attribute equal to the specified ID, and it appears<br />

before an element with the given ID in the document, IE returns the form element. Here’s an example:<br />

< input type=”text” name=”myElement” value=”Text field” ><br />

< div id=”myElement” > A div < /div ><br />

Using this HTML, a call to document.getElementById() in IE 7 returns a reference to the < input ><br />

element, whereas the same call returns a reference to the < div > element in all other browsers. To avoid<br />

this issue in IE, it ’ s best to ensure that form fields don ’ t have name attributes that are equivalent to other<br />

element IDs.<br />

The getElementsByTagName() method is another commonly used method for retrieving element<br />

references. It accepts a single argument — the tag name of the elements to retrieve — and returns a<br />

NodeList containing zero or more elements. In HTML documents, this method returns an<br />

HTMLCollection object, which is very similar to a NodeList in that it is considered a “ live ” collection.<br />

For example, the following code retrieves all < img > elements in the page and returns an<br />

HTMLCollection :<br />

var images = document.getElementsByTagName(“img”);<br />

This code stores an HTMLCollection object in the images variable. As with NodeList objects, items in<br />

HTMLCollection objects can be accessed using bracket notation or the item() method. The number of<br />

elements in the object can be retrieved via the length property, as this example demonstrates:<br />

alert(images.length);<br />

alert(images[0].src);<br />

alert(images.item(0).src);<br />

//output the number of images<br />

//output the src attribute of the first image<br />

//output the src attribute of the first image<br />

273

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!