W3cubDocs

/Web APIs

Element: getElementsByTagName() method

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

js

getElementsByTagName(tagName)

Parameters

  • tagName is the qualified name to look for. The special string "*" represents all elements. For compatibility with XHTML, lower-case should be used.

Return value

A live HTMLCollection of elements with a matching tag name, in the order they appear. If no elements are found, the HTMLCollection is empty.

Examples

js

// Check the status of each data cell in a table
const table = document.getElementById("forecast-table");
const cells = table.getElementsByTagName("td");

for (const cell of cells) {
  const status = cell.getAttribute("data-status");
  if (status === "open") {
    // Grab the data
  }
}

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
getElementsByTagName
1Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
12
1Before Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.
5.5
8Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
1Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
4.4Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
18Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
4Before Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.
10.1
1Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
1.0Initially, this method was returning a NodeList; it was then changed to reflect the spec change.
all_elements_selector 1 12 1 6 ≤15 1 4.4 18 4 ≤14 1 1.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName