W3cubDocs

/DOM

element.getElementsByTagName

The Element.getElementsByTagName() method returns a live HTMLCollection of elements with the given tag name. All descendants of the specified element are searched, but not the element itself. The returned list is live, which means it updates itself with the DOM tree automatically. Therefore, there is no need to call Element.getElementsByTagName() with the same element and arguments repeatedly if the DOM changes in between calls.

When called on an HTML element in an HTML document, getElementsByTagName lower-cases the argument before searching for it. This is undesirable when trying to match camel-cased SVG elements (such as <linearGradient>) in an HTML document. Instead, use Element.getElementsByTagNameNS(), which preserves the capitalization of the tag name.

Element.getElementsByTagName is similar to Document.getElementsByTagName(), except that it only searches for elements that are descendants of the specified element.

Syntax

elements = element.getElementsByTagName(tagName)
  • elements is a live HTMLCollection of elements with a matching tag name, in the order they appear. If no elements are found, the HTMLCollection is empty.
  • element is the element from where the search starts. Only the element's descendants are included, 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 status of each data cell 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 compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 1
1
Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
Yes Yes
Yes
Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.
5.5 Yes
Yes
Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
Yes
Yes
Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
getElementsByTagName(*) 1 Yes Yes 6 Yes Yes
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support Yes ? Yes Yes
Yes
Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.
Yes Yes Yes
getElementsByTagName(*) Yes ? Yes Yes
Yes
Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.
Yes Yes ?

© 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