Sometimes, we want to use getElementsByClassName() with two classes with JavaScript.
In this article, we’ll look at how to use getElementsByClassName() with two classes with JavaScript.
How to use getElementsByClassName() with two classes with JavaScript?
To use getElementsByClassName() with two classes with JavaScript, we can use the querySelectorAll
method.
For instance, we write
const els = document.querySelectorAll(".a,.b");
to call document.querySelectorAll
to select all elements on the page with classes a
and b
.
We separate each class with commas in the selector.
Conclusion
To use getElementsByClassName() with two classes with JavaScript, we can use the querySelectorAll
method.