class exists javascript code example

Example 1: check if a class exists javascript

const element = document.querySelector("#box");

element.classList.contains("active");

Example 2: class exists javascript

<div class="secondary info">Item</div>Code language: HTML, XML (xml)

Example 3: class exists javascript

const div = document.querySelector('.info');
div.classList.contains('secondary'); // trueCode language: JavaScript (javascript)

Example 4: check if a class exists javascript

<div id="box" class="active"></div>