This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s).
When called on the document object, the complete document is searched, including the root node. You may also call getElementsByClassName() on any element; it will return only elements which are descendants of the specified root element with the given class name(s).
Warning: This is a live HTMLCollection. Changes in the DOM will reflect in the array as the changes occur. If an element selected by this array no longer qualifies for the selector, it will automatically be removed. Be aware of this for iteration purposes.
getElementsByClassName(names)
namesA string representing the class name(s) to match; multiple class names are separated by whitespace.
A live HTMLCollection of found elements.
Get all elements that have a class of 'test':
document.getElementsByClassName("test");
Get all elements that have both the 'red' and 'test' classes:
document.getElementsByClassName("red test");
Get all elements that have a class of 'test', inside of an element that has the ID of 'main':
document.getElementById("main").getElementsByClassName("test");
Get the first element with a class of 'test', or undefined if there is no matching element:
document.getElementsByClassName("test")[0];
We can also use methods of Array.prototype on any HTMLCollection by passing the HTMLCollection as the method's this value. Here we'll find all div elements that have a class of 'test':
const testElements = document.getElementsByClassName("test");
const testDivs = Array.prototype.filter.call(
testElements,
(testElement) => testElement.nodeName === "DIV",
);
This is the most commonly used method of operation.
<div id="parent-id"> <p>hello world 1</p> <p class="test">hello world 2</p> <p>hello world 3</p> <p>hello world 4</p> </div>
const parentDOM = document.getElementById("parent-id");
const test = parentDOM.getElementsByClassName("test"); // a list of matching elements, *not* the element itself
console.log(test); // HTMLCollection[1]
const testTarget = parentDOM.getElementsByClassName("test")[0]; // the first element, as we wanted
console.log(testTarget); // <p class="test">hello world 2</p>
document.getElementsByClassName works very similarly to document.querySelector and document.querySelectorAll. Only elements with ALL of the classNames specified are selected.
<span class="orange fruit">Orange Fruit</span> <span class="orange juice">Orange Juice</span> <span class="apple juice">Apple Juice</span> <span class="foo bar">Something Random</span> <textarea id="resultArea"></textarea>
// getElementsByClassName only selects elements that have both given classes
const allOrangeJuiceByClass = document.getElementsByClassName("orange juice");
let result = "document.getElementsByClassName('orange juice')";
for (const el of allOrangeJuiceByClass) {
result += `\n ${el.textContent}`;
}
// querySelector only selects full complete matches
const allOrangeJuiceQuery = document.querySelectorAll(".orange.juice");
result += "\n\ndocument.querySelectorAll('.orange.juice')";
for (const el of allOrangeJuiceQuery) {
result += `\n ${el.textContent}`;
}
document.getElementById("resultArea").value = result;
| Specification |
|---|
| DOM> # ref-for-dom-document-getelementsbyclassname①> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
getElementsByClassName |
1 | 12 | 3 | 9.5 | 3.1 | 18 | 4 | 10.1 | 2 | 1.0 | 4.4 | 2 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName