23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

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

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

CHAPTER 7 ■ TRAVERSING AND MODIFYING THE DOM TREE<br />

278<br />

getElementById. Second, if no element in your markup has the desired id, then<br />

Document.getElementById() returns null. This is what you ought to expect inasmuch as an Element node<br />

is of the object datatype, which is to say its value is stored on the heap. Finally, in the event that more<br />

than one element in your markup has the desired id, which is a markup error,<br />

Document.getElementById() may return null, or it may randomly return one of the elements. Note that,<br />

other than in Internet Explorer prior to version 8, the id parameter is case sensitive in all relevant<br />

browsers. Therefore, for our with an id of "twitter", passing Document.getElementById() the<br />

parameter "Twitter" returns null in Firefox, Safari, Opera, and Internet Explorer 8+. On the other hand,<br />

Internet Explorer 7 or older returns the by mistake. With this in mind, do not give different elements<br />

id attributes that vary only by case, such as "sprite" and "Sprite", since Internet Explorer may consider<br />

those to be the same id.<br />

Finding Elements by Their Tag Names<br />

Oftentimes, you will want to do some stuff to elements with the same tag name. Say you want to turn<br />

every element with a class of "sprite" into a CSS sprite. Rather than traverse the DOM tree with a<br />

function like traverseTree(), simply pass either Document.getElementsByTagName() or<br />

Element.getElementsByTagName() a string indicating the tag name of the elements you want, and<br />

JavaScript will return a NodeList containing those elements for your scripting pleasure. Take care to note<br />

that it is Elements, plural, in getElementsByTagName(), but it’s Element, singular, in getElementById().<br />

So, you can call getElementsByTagName() on either an Element or a Document node. What’s the<br />

difference? If you want JavaScript to return all the elements in your markup that have the same tag<br />

name, go with Document.getElementsByTagName(). On the other hand, if you just want descendants of a<br />

certain element that have the same tag name, meaning you just want to work with a branch of the DOM<br />

tree rather than the whole thing, then turn to Element.getElementsByTagName().<br />

Click Refresh in Firefox (to revert the second to "Facebook") and then click Clear in both<br />

Firebug panels. Let’s try Document.getElementsByTagName() first:<br />

var myElements = document.getElementsByTagName("a");<br />

myElements;<br />

// [a www.twitter.com, a www.facebook.com, a www.flickr.com, a www.linkedin.com]<br />

JavaScript returns a NodeList containing every Element node from the DOM tree. myElements<br />

contains four members. Now let’s try Element.getElementsByTagName() on just a branch of the tree:<br />

myElements = document.getElementById("twitter").getElementsByTagName("a");<br />

// [a www.twitter.com]<br />

Verify your work with Figure 7–12.

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

Saved successfully!

Ooh no, something went wrong!