An alternative way to access elements on the current page is to access them through the tag names. Whenever you have to work on a set of elements that are represented by the same HTML tags (for instance, all list items or all paragraphseverything that is not represented by another property of the JavaScript document object), the method document.getElementsByTagName() can be used. You provide the tag name and get an array of all elements, which you can then process further. The following code just accesses all <p> elements and counts them: Accessing Elements By Tag Name (getelementsbytagname.html)
The output of the preceding code is [object HTMLCollection] (2 elements); Internet Explorer once again gives less information, outputting [object] (2 elements).
|