Web/API/Element/getElementsByTagName

From Get docs


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
const table = document.getElementById('forecast-table'); 
const cells = table.getElementsByTagName('td');

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

Specifications

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

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
getElementsByTagName

Chrome Full support 1

Notes'

Full support 1

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

Edge

Full support 12

Firefox Full support 1

Notes'

Full support 1

Notes'

Notes' Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.

IE

Full support 5.5

Opera Full support 8

Notes'

Full support 8

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

Safari Full support 6

Notes'

Full support 6

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

WebView Android Full support 1

Notes'

Full support 1

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

Chrome Android Full support 18

Notes'

Full support 18

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

Firefox Android Full support 4

Notes'

Full support 4

Notes'

Notes' Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.

Opera Android

Full support 10.1

Safari iOS Full support 6

Notes'

Full support 6

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

Samsung Internet Android Full support 1.0

Notes'

Full support 1.0

Notes'

Notes' Initially, this method was returning a NodeList; it was then changed to reflect the spec change.

getElementsByTagName(*) Chrome

Full support 1

Edge

Full support 12

Firefox

Full support Yes

IE

Full support 6

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android Full support Yes

Notes'

Full support Yes

Notes'

Notes' Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support Yes

Legend

Full support  
Full support
See implementation notes.'
See implementation notes.