To check if an element contains a class, you use the contains()
method of the classList
property of the element:
element.classList.contains(className);
Suppose you have the following <div>
element:
<div class="secondary info">Item</div>
To check if the <div>
element contains the secondary
class, you use the following code:
const div = document.querySelector('.info');
div.classList.contains('secondary'); // true
The following returns false because the <div>
element doesn’t have the error
class:
const div = document.querySelector('.info');
div.classList.contains('error'); // false
Source
https://www.javascripttutorial.net/dom/css/check-if-an-element-contains-a-class/