Using plain JavaScript (not jQuery), Is there any way to check if an element contains a class?
Currently, I\'m doing this:
element.matches(selectorString)
According to MDN Web Docs:
The
Element.matches()
method returnstrue
if the element would be selected by the specified selector string; otherwise, returnsfalse
.
Therefore, you can use Element.matches() to determine if an element contains a class.
const element = document.querySelector('#example');
console.log(element.matches('.foo')); // true
View Browser Compatibility