Get all elements in the document with the specified tag name:
More "Try it Yourself" examples below.
The getElementsByTagName() method returns a collection of all elements in the document with the specified tag name, as a NodeList object.
The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.
Tip: The parametervalue "*" returns all elements in the document.
Tip: You can use the length property of the NodeList object to determine the number of elements with the specified tag name, then you can loop through all elements and extract the info you want.
The numbers in the table specifies the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
getElementsByTagName() | 1.0 | 6.0 | 3.0 | 3.0 | 9.5 |
Parameter | Type | Description |
---|---|---|
tagname | String | Required. The tagname of the elements you want to get |
DOM Version: | Core Level 1 Document Object |
---|---|
Return Value: | A NodeList object, representing a collection of elements with the specified tag name. The elements in the returned collection are sorted as they appear in the source code. |
Find out how many <li> elements there are in the document (using the length property of the NodeList object):
Change the HTML content of the first <p> element (index 0) in the document:
Change the background color of all <p> elements in the document:
Using the "*" parameter.
Get all elements in the document:
JavaScript Reference: element.getElementsByTagName()
JavaScript Tutorial: JavaScript HTML DOM Node List