Element: getElementsByTagName() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheElement.getElementsByTagName() method returns a liveHTMLCollection of elements with the giventag name.
All descendants of thespecified element are searched, but not the element itself. The returned list islive, which means it updates itself with the DOM tree automatically.Therefore, there is no need to callElement.getElementsByTagName() withthe same element and arguments repeatedly if the DOM changes in between calls.
When called on an HTML element in an HTML document,getElementsByTagNamelower-cases the argument before searching for it. This is undesirable when trying tomatchcamel-cased SVG elements (such as<linearGradient>)in an HTML document. Instead, useElement.getElementsByTagNameNS(),which preserves the capitalization of the tag name.
Element.getElementsByTagName is similar toDocument.getElementsByTagName(), except that it only searches forelements that are descendants of the specified element.
In this article
Syntax
getElementsByTagName(tagName)Parameters
tagNameThe qualified name to look for. The special string
"*"represents all elements. For compatibility with XHTML, lower-caseshould be used.
Return value
AliveHTMLCollection of elements with a matching tag name, in the order they appear. If no elements are found, theHTMLCollection is empty.
Examples
// Check the status of each data cell in a tableconst 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
| Specification |
|---|
| DOM> # dom-element-getelementsbytagname> |