check classlist javascript code example

Example 1: javascript check if element has class

if(document.getElementById("myElmentID").classList.contains("hidden")){
// I have the 'hidden' class
}

Example 2: js check for class in classList

element.classList.contains(className);

Example 3: add multiple class list at once in js

elem.classList.add("first");
elem.classList.add("second");
elem.classList.add("third");

is equal to :

elem.classList.add("first","second","third");

Example 4: if classlist contains js

element.classList.contains(class);

Example 5: css class list

var myElement = document.getElementById("myElementID");
myElement.classList.add("style1 style2");
myElement.classList.remove("style1 style2");

Example 6: classList has class

if (document.body.classList.contains('thatClass')) {
    // do some stuff
}

Tags:

Misc Example