Definition and Usage
The getElementsByClassName() function returns an HTMLCollection object that contains all elements matching the specified class 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>getElementsByClassName()</title>
</head>
<body>
<ul>
<li class="a">Hamburger</li>
<li class="a">Hot Dog</li>
<li class="a">French Fries</li>
</ul>
<script src="class-name.js"></script>
</body>
</html>
const liElements = document.getElementsByClassName("a");
console.log(liElements); // HTMLCollection(3) [li.a, li.a, li.a]
/* 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(li => {
console.log(li.textContent);
});
// Output: "Hamburger" "Hot Dog" "French Fries"
/* Approach 4: Converting to an array using Array.from() */
const liElementsArray = Array.from(liElements);
liElementsArray.forEach(li => {
console.log(li.textContent);
});
// Output: "Hamburger" "Hot Dog" "French Fries"
The HTMLCollection returned by the getElementsByClassName() 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.getElementsByClassName(names)
// Within a specific element
element.getElementsByClassName(names)
Parameters
names |
|
|---|
Return Value
- Returns an
HTMLCollectionobject containing all elements in the HTML document that match the specified class name. - If no matching elements are found, it returns an empty
HTMLCollectionobject.
Return Value Example
Finding Elements with Multiple Class Names
You can find elements that have multiple class names.
Simply specify multiple class names separated by spaces to find elements that contain all of them.
The following example finds elements that have both the class names a and b.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getElementsByClassName() - Finding Elements with Multiple Class Names</title>
</head>
<body>
<ul>
<li class="a">Hamburger</li>
<li class="a b c">Hot Dog</li>
<li class="a b">French Fries</li>
</ul>
<script src="test-1.js"></script>
</body>
</html>
/* Find elements that have both class names a and b */
const liElements = document.getElementsByClassName("a b");
for (const liElement of liElements) {
console.log(liElement.textContent);
}
// Output: "Hot Dog" "French Fries"
To Access the n-th Matching Element
To access the n-th matching element, you can use the following syntax.
getElementsByClassName(name)[n]
The getElementsByClassName() 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 element with the class name a in an HTML document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>getElementsByClassName(name)[n]</title>
</head>
<body>
<ul>
<li class="a">Hamburger</li>
<li class="a">Hot Dog</li>
<li class="a">French Fries</li>
</ul>
<script>
const liElements = document.getElementsByClassName("a");
/* The first element with the class name a 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>getElementsByClassName().length</title>
</head>
<body>
<ul>
<li class="a">Hamburger</li>
<li class="a">Hot Dog</li>
<li class="a">French Fries</li>
</ul>
<script>
const liElements = document.getElementsByClassName("a");
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 getElementsByClassName() function is a method that can be used not only on the document object but also within specific element (Element) objects.
The following example shows how to find all elements with the class name a within an element whose id attribute is "chinese-menu".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>element.getElementsByClassName()</title>
</head>
<body>
<h1>element.getElementsByClassName()</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 class="a">Orange Chicken</li>
<li class="a">Lo Mein</li>
<li class="a">Spring Rolls</li>
</ul>
</div>
<script>
const chineseMenu = document.getElementById("chinese-menu");
// Find all elements with the class name "a" within the element with the id "chinese-menu"
const chineseMenuLiElements = chineseMenu.getElementsByClassName("a");
// Output the text content of the first element among the found elements
console.log(chineseMenuLiElements[0].textContent); // Output: "Orange Chicken"
</script>
</body>
</html>
Specifications
| Specification | |
|---|---|
document.getElementsByClassName()
|
DOM Standard #ref-for-dom-document-getelementsbyclassname① |
element.getElementsByClassName()
|
DOM Standard #ref-for-dom-element-getelementsbyclassname |
Browser Compatibility
| Method |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
getElementsByClassName()
|
1 | 12 | 3 | 3.1 |