Definition and Usage
The getElementsByTagName() function returns an HTMLCollection object that contains all elements matching the specified tag name.
If no matching elements are found, it returns an empty HTMLCollection object.
You can iterate through the elements in the returned HTMLCollection object and perform a specific task on each element.
HTMLCollection- A collection of elements selected from an HTML document, arranged in document order.
This collection, commonly referred to as a DOM collection, can be treated like an array-like structure, allowing elements to be accessed in a way similar to array items and making it easier to manipulate elements on a web page.
Note
HTMLCollection is an array-like object that can also be iterated over using a for...of statement.
It is also a live object, meaning it automatically reflects changes in the DOM.
The length property can be used to return the number of items in the array-like HTMLCollection.
Basic Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getElementsByTagName()</title>
</head>
<body>
<ul>
<li>Hamburger</li>
<li>Hot Dog</li>
<li>French Fries</li>
</ul>
<script src="tag-name.js"></script>
</body>
</html>
const liElements = document.getElementsByTagName("li"); // Select all li elements
console.log(liElements); // HTMLCollection(3) [li, li, li]
/* Approach 1: Using for...of */
for (const liElement of liElements) {
console.log(liElement.textContent);
}
// Output: "Hamburger" "Hot Dog" "French Fries"
/* Approach 2: Using a standard for loop (The length property is accessible) */
for (let i = 0; i < liElements.length; i++) {
console.log(liElements[i].textContent);
}
// Output: "Hamburger" "Hot Dog" "French Fries"
/* Approach 3: Converting to an array using the spread operator (...) */
const arr = [...liElements];
arr.forEach(item => {
console.log(item.textContent);
});
// Output: "Hamburger" "Hot Dog" "French Fries"
/* Approach 4: Converting to an array using Array.from() */
const liElementsArray = Array.from(liElements);
liElementsArray.forEach(item => {
console.log(item.textContent);
});
// Output: "Hamburger" "Hot Dog" "French Fries"
The HTMLCollection returned by the getElementsByTagName() function is not an array, so the forEach() function cannot be used directly.
The HTMLCollection is an array-like object, but because it is not an actual array, it does not support array methods.
However, by converting it into an array using Array.from() or the spread syntax (...), you can use the forEach() function.
Code Explanation
The forEach() function is used to iterate over an array and process each element with a callback function.
Code Explanation
The for loop is a statement used to repeat a block of code a specific number of times or over a defined range. It can be used to iterate over array-like objects.
Code Explanation
The textContent property is used to get or set the text content of an HTML element.
Syntax
// In the entire HTML document
document.getElementsByTagName(name)
// Within a specific element
element.getElementsByTagName(name)
Parameters
name |
A string representing the tag name of the elements to search for. This value is case-insensitive.
The special character "*" represents all elements. |
|---|
Return Value
- Returns an
HTMLCollectionobject containing all elements in the HTML document that match the specified tag name. - If no matching elements are found, it returns an empty
HTMLCollectionobject.
Return Value Example
When the Parameter Value Is the Special Character "*"
When the parameter value is the special character "*", all elements are returned.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getElementsByTagName("*")</title>
</head>
<body>
<h1>getElementsByTagName("*")</h1>
<ul>
<li>Hamburger</li>
<li>Hot Dog</li>
<li>French Fries</li>
</ul>
<script>
const allElements = document.getElementsByTagName("*");
console.log(allElements);
// HTMLCollection(11) [html, head, meta, title, body, h1, ul, li, li, li, script]
</script>
</body>
</html>
To Access the n-th Matching Element
To access the n-th matching element, you can use the following syntax.
getElementsByTagName(name)[n]
The getElementsByTagName() function returns an HTMLCollection, which is an array-like object that can be accessed using indexes just like an array.
The following example demonstrates how to access the first <li> element in an HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getElementsByTagName()[n]</title>
</head>
<body>
<h1>getElementsByTagName()[n]</h1>
<ul>
<li>Hamburger</li>
<li>Hot Dog</li>
<li>French Fries</li>
</ul>
<script>
const liElements = document.getElementsByTagName("li"); // Select all li elements
/* The first <li> element in the HTML document */
console.log(liElements[0].textContent); // Index starts at 0
// Output: "Hamburger"
</script>
</body>
</html>
Check the Number of Matching Elements
The number of matching elements can be easily checked using the length property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getElementsByTagName().length</title>
</head>
<body>
<h1>getElementsByTagName().length</h1>
<ul>
<li>Hamburger</li>
<li>Hot Dog</li>
<li>French Fries</li>
</ul>
<script>
const liElements = document.getElementsByTagName("li"); // Select all li elements
console.log(liElements.length); // Output: 3
</script>
</body>
</html>
Finding Elements Within a Specific Element
The getElementById() function is a method used only on the document object. However, the getElementsByTagName() function is a method that can be used not only on the document object but also within specific element (Element) objects.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>element.getElementsByTagName()</title>
</head>
<body>
<h1>element.getElementsByTagName()</h1>
<h2>Diner Menu</h2>
<ul>
<li>Hamburger</li>
<li>Hot Dog</li>
<li>French Fries</li>
</ul>
<h2>Chinese Restaurant Menu</h2>
<div id="chinese-menu">
<ul>
<li>Orange Chicken</li>
<li>Lo Mein</li>
<li>Spring Rolls</li>
</ul>
</div>
<script>
const chineseMenu = document.getElementById("chinese-menu");
// Select all li elements inside the element with the id "chinese-menu"
const chineseMenuLiElements = chineseMenu.getElementsByTagName("li");
// Output the text content of the first element among all found elements
console.log(chineseMenuLiElements[0].textContent); // Output: "Orange Chicken"
</script>
</body>
</html>
Specifications
| Specification | |
|---|---|
document.getElementsByTagName()
|
DOM Standard #ref-for-dom-document-getelementsbytagname① |
element.getElementsByTagName()
|
DOM Standard #dom-element-getelementsbytagname |
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
getElementsByTagName()
|
1 | 12 | 1 | 1 |
See Also
- JavaScript getElementById() Function – A Complete Guide
- JavaScript getElementsByClassName() Function – A Complete Guide
- JavaScript querySelectorAll() Function – A Complete Guide
- JavaScript querySelector() Function – A Complete Guide
- JavaScript closest() Function – Finding the Closest Ancestor Element