contains method of the classList object
The contains method of the
classList
object checks for the presence of
an element's CSS class.
Syntax
element.classList.contains(class);
Example
We check if an element has
the class ggg:
<p id="elem" class="www ggg zzz"></p>
let elem = document.querySelector('#elem');
let contains = elem.classList.contains('ggg');
console.log(contains);
The code execution result:
true
See also
-
the
classList.addmethod
that adds a given class -
the
classList.removemethod
that removes a given class -
the
classList.togglemethod
which toggles a given class