Menu Close

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

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

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.

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.

Posted in JavaScript

You can also read...