Visit Mozilla.org

DOM:element.getElementsByTagName

From MDC

« Gecko DOM Reference

Contents

[edit] Summary

Returns a list of elements with the given tag name. The subtree underneath the specified element is searched, excluding the element itself.

[edit] Syntax

 elements = element.getElementsByTagName(tagName) 
  • elements is a live NodeList of found elements in the order they appear in the subtree.
  • element is the element from where the search should start. Note that only the descendants of this element are included in the search, but not the element itself.
  • tagName is the qualified name to look for. The special string "*" represents all elements.

In Firefox 2 (Gecko 1.8.1) and earlier this method didn't work correctly if the subtree had elements with namespace prefix in the tag name (See bug 206053 for details.)

It's recommended to use element.getElementsByTagNameNS when dealing with multi-namespace documents.

[edit] Example

// check the alignment on a number of cells in a table. 
var table = document.getElementById("forecast-table"); 
var cells = table.getElementsByTagName("td"); 
for (var i = 0; i < cells.length; i++) { 
    status = cells[i].getAttribute("status"); 
    if ( status == "open") { 
        // grab the data 
    }
}

[edit] Notes

element.getElementsByTagName is similar to document.getElementsByTagName, except that its search is restricted to those elements which are descendants of the specified element.

[edit] Specification

DOM Level 2 Core: Element.getElementsByTagName