W3cubDocs

/DOM

element.getElementsByTagName

The Element.getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name. The subtree underneath the specified element is searched, excluding the element itself. The returned list is live, meaning that it updates itself with the DOM tree automatically. Consequently, there is no need to call several times Element.getElementsByTagName() with the same element and arguments.

When called on an HTML element in an HTML document, getElementsByTagName put its argument in lower case before proceeding. This is undesirable when trying to match camel-cased SVG elements in a sub-tree in an HTML document. Element.getElementsByTagNameNS() works in that case.

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

Syntax

elements = element.getElementsByTagName(tagName)
  • elements is a live HTMLCollection of found elements in the order they appear in the sub-tree. If no elements were found, the HTMLCollection is empty.
  • 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. For compatibility with XHTML, lower-case should be used.

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++) { 
    var status = cells[i].getAttribute("data-status"); 
    if ( status == "open" ) { 
        // grab the data 
    }
}

Specifications

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 [2] (Yes) (Yes) [1] 5.5 (Yes) [2] (Yes) [2]
getElementsByTagName("*") 1.0 (Yes) (Yes) 6.0 (Yes) (Yes)
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) [1] (Yes) (Yes) (Yes)

[1] Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the spec change.

[2] Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/element/getElementsByTagName