HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Get all elements with the tag name li"
The getElementsByTagName()
method returns a collection of all elements with a specified tag name.
The getElementsByTagName()
method returns an HTMLCollection.
The getElementsByTagName()
property is read-only.
getElementsByTagName("*")
returns all elements in the document.
An HTMLCollection is an array-like collection (list) of HTML elements.
The elements in a collection can be accessed by index (starts at 0).
Parameter | Description |
tagname | Required. The tagname of the elements. |
Type | Description |
Object | An HTMLCollection object. A collection of elements with a specified tag name. The elements are sorted as they appear in the document. |
The number of <li> elements in the document:
let numb = document.getElementsByTagName("li").length;
Change the background color of all <p> elements:
const collection = document.getElementsByTagName("P");
for (let i = 0; i < collection.length; i++) {
collection[i].style.backgroundColor = "red";
}
JavaScript Reference: element.getElementsByTagName()
JavaScript Tutorial: JavaScript HTML DOM Node List
document.getElementsByTagName()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |