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

JavaScript
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

JavaScript
// 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

Specification Status Comment
DOM
The definition of 'Element.getElementsByTagName()' in that specification.
Living Standard Changed the return value from NodeList to HTMLCollection
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Element.getElementsByTagName()' in that specification.
Recommendation No change from Document Object Model (DOM) Level 2 Core Specification
Document Object Model (DOM) Level 2 Core Specification
The definition of 'Element.getElementsByTagName()' in that specification.
Recommendation No change from Document Object Model (DOM) Level 1 Specification
Document Object Model (DOM) Level 1 Specification
The definition of 'Element.getElementsByTagName()' in that specification.
Recommendation Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 [2] (Yes) [1] 5.5 (Yes) [2] (Yes) [2]
getElementsByTagName("*") 1.0 (Yes) 6.0 (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (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.

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/element/getelementsbytagname

API DOM Element Élément Method Reference Référence