Last updated: Mar 5, 2024
Reading timeยท3 min

Use the getElementsByClassName method to get elements by multiple class
names.
The method returns an array-like object containing all the elements that have all of the given class names.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> <style> .green { background-color: greenyellow; } .orange { background-color: orange; } </style> </head> <body> <div class="box green">Box 1</div> <div class="box green">Box 2</div> <div class="box orange">Box 3</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ Get all elements with classes `box` AND `green` const elements1 = document.getElementsByClassName('box green'); console.log(elements1); // ๐๏ธ [div.box.green, div.box.green] // ---------------------------------------------------- // โ Get all elements with classes `box` AND `green` const elements2 = document.querySelectorAll('.box.green'); console.log(elements2); // ๐๏ธ [div.box.green, div.box.green] // ---------------------------------------------------- // โ Get all elements with at least one of the classes const elements3 = document.querySelectorAll('.box, .green'); // ๐๏ธ [div.box.green, div.box.green, div.box.orange] console.log(elements3);

querySelector method instead of querySelectorAll.We used the document.getElementsByClassName() method in the first example.
// โ Get all elements with classes `box` AND `green` const elements1 = document.getElementsByClassName('box green'); console.log(elements1); // ๐๏ธ [div.box.green, div.box.green]
The method takes one or more class names as a parameter and returns an
HTMLCollection with the elements that contain all the provided class names.
If you need to treat the HTMLCollection as an array, e.g. to use methods like
forEach(), convert it to an array by using the Array.from() method.
const elements1 = Array.from( document.getElementsByClassName('box green') ); elements1.forEach(element => { console.log(element); });

The Array.from() method creates a new, shallow-copied array from the provided iterable.
The second example uses the
document.querySelectorAll method to get
the elements that have the classes box and green.
// โ Get all elements with classes `box` AND `green` const elements2 = document.querySelectorAll('.box.green'); console.log(elements2); // ๐๏ธ [div.box.green, div.box.green]

NodeList containing the elements that match the selectors.If you need to get the first element that has the classes box and green,
use the querySelector method instead.
// โ Get the first element with classes `box` AND `green` const elements2 = document.querySelector('.box.green'); console.log(elements2); // ๐๏ธ div.box.green

The third example uses the querySelectorAll method to get the elements that
have at least one of the classes box and green.
// โ Get all elements with at least one of classes `box` or `green` const elements3 = document.querySelectorAll('.box, .green'); console.log(elements3); // ๐๏ธ [div.box.green, div.box.green, div.box.orange]

You can pass as many comma-separated selectors to the querySelectorAll method
as necessary.
You can also select the elements that have one of the classes but not the other.
const elements4 = document.querySelectorAll( '.box:not(.orange)' ); console.log(elements4); // ๐๏ธ [div.box.green, div.box.green]

The example selects the elements that have a class of box but don't have the
class of orange.
You can also narrow the results down by adding a specific tag name to the selector.
// โ Get all DIV elements with classes `box` AND `green` const elements5 = document.querySelectorAll('div.box.green'); console.log(elements5); // ๐๏ธ [div.box.green, div.box.green]

The example shows how to get all div elements that have both classes - box
and green.
This selector would not match any span or p elements that have the same
classes.
You can learn more about the related topics by checking out the following tutorials: