English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
document.getElementsByTagName(tag)
var x = document.getElementsByTagName("p");Test and See‹/›
All browsers fully support the getElementsByTagName() method:
Method | |||||
getElementsByTagName() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
tag | The tag name of the element you want to retrieve. |
Return Value: | Returns an array-like object of all elements with the specified tag name |
---|---|
DOM Version: | DOM Level1 |
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‹/›
DOM Document querySelector()Methods
DOM Document querySelectorAll()Methods
DOM Document getElementById()Methods
DOM Document getElementsByClassName()Methods