English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM getElementsByTagName() method

HTML DOM Document Object

getElementsByTagName()The method can return a collection of objects with the specified tag name.

The elements in the returned list are sorted in the order they appear in the source code.

You can access the returned list by index number. The index starts from 0.

Using an array'slengthThe property determines the number of elements with the specified tag name, and then you can iterate over all elements to extract the required information.

Syntax:

document.getElementsByTagName(tag)
var x = document.getElementsByTagName("p");
Test and See‹/›

Browser compatibility

All browsers fully support the getElementsByTagName() method:

Method
getElementsByTagName()YesYesYesYesYes

Parameter Value

ParameterDescription
tagThe tag name of the element you want to retrieve.

Technical Details

Return Value:Returns an array-like object of all elements with the specified tag name
DOM Version:DOM Level1

More Examples

Change the background color of all paragraph elements in the document:

var x = document.getElementsByTagName("p");
for (let i = 0; i < x.length; i++) {
   x[i].style.backgroundColor = "coral";
}
Test and See‹/›

Related References

DOM Document querySelector()Methods

DOM Document querySelectorAll()Methods

DOM Document getElementById()Methods

DOM Document getElementsByClassName()Methods

HTML DOM Document Object