element classlist code example
Example 1: first remove active class from classlist and append to current element using javascript
function myFunction(e) {
if (document.querySelector('#navList a.active') !== null) {
document.querySelector('#navList a.active').classList.remove('active');
}
e.target.className = "active";
}
Example 2: js classlist
classList.item(index);
classList.contains(token);
classList.add(token1[, ...tokenN]);
classList.remove(token1[, ...tokenN]);
classList.replace(oldToken, newToken);
classList.supports(token);
classList.toggle(token[, force]);
classList.entries();
classList.forEach(callback[ ,thisArg]);
classList.keys();
classList.values();
Example 3: add multiple classList
element.classList.add(class1,class2,class3...);
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')) {
}