How to check if an element contains a class in JavaScript?

Spread the love

Sometimes, we want to check if an element contains a class in JavaScript.

In this article, we’ll look at how to check if an element contains a class in JavaScript.

How to check if an element contains a class in JavaScript?

To check if an element contains a class in JavaScript, we use classList.

For instance, we write

const hasClassName = element.classList.contains(className);

to check if the className class exists in the element with classList.contains.

Conclusion

To check if an element contains a class in JavaScript, we use classList.

Leave a Reply

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