Categories
JavaScript Answers

How to use getElementsByClassName() with two classes with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *