javascript pocket guide [burdette]

Page 166

Chapter 10: The DOM

139

Finding Nodes Walking the DOM is useful for finding elements that are close to one another, but if you need an element deep in the DOM tree, it’s much easier to find it with one of these retrieval methods.

getElementById(domId) The getElementById() method is one of the most common DOM methods. It returns a single element node that has the id attribute of domId. <div id="content"> <div id="sidebar"></div> </div> document.getElementById("content"); <div id="content">

getElementsByTagName(name) The getElementsByTagName() method returns a collection of element nodes with the tag name of name. All element nodes have these retrieval methods, so you can look for a node inside another node. var content = document.getElementById("content"); content.getElementsByTagName("div"); [ <div class="sidebar"> ]

getElementsByClassName(name) Not available in Internet Explorer Modern browsers provide a method similar to getElementsByTagName() that retrieves elements by their class attribute. It even finds elements using combinations of classes when separated by a space.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.