0
Q:

classlist.add


 document.getElementById("myDIV").classList.add("mystyle"); 
5
classList.item(index); // Returns the item in the list by its index, or undefined if index is greater than or equal to the list's length
classList.contains(token); // Returns true if the list contains the given token, otherwise false.
classList.add(token1[, ...tokenN]); // Adds the specified token(s) to the list.
classList.remove(token1[, ...tokenN]); // Removes the specified token(s) from the list.
classList.replace(oldToken, newToken); // Replaces token with newToken.
classList.supports(token); // Returns true if a given token is in the associated attribute's supported tokens.
classList.toggle(token[, force]); // Removes token from the list if it exists, or adds token to the list if it doesn't. Returns a boolean indicating whether token is in the list after the operation.
classList.entries(); // Returns an iterator, allowing you to go through all key/value pairs contained in this object.
classList.forEach(callback[ ,thisArg]); // Executes a provided callback function once per DOMTokenList element.
classList.keys(); // Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
classList.values(); // Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
7

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");
1
var myElement = document.getElementById("myElementID");
myElement.classList.add("style1 style2");
myElement.classList.remove("style1 style2");
2
Add multiple classes to a <div> element:
document.getElementById("myDIV").classList.add("mystyle", "anotherClass", "thirdClass");

Remove a class from a <div> element:
document.getElementById("myDIV").classList.remove("mystyle");

Remove multiple classes from a <div> element:
document.getElementById("myDIV").classList.remove("mystyle", "anotherClass", "thirdClass");

Toggle between two classes for a <div> element:
document.getElementById("myDIV").classList.toggle("newClassName");

Find out if an element has a "mystyle" class:
document.getElementById("myDIV").classList.contains("mystyle");
2
if (document.body.classList.contains('thatClass')) {
    // do some stuff
}
0
// .classList method returns an array of the classes attached to the element it is called with.
document.getElementById("myDIV").classList
// It returns
// [ 'className1', 'className2', ... ]
1
//get array of class names in Javascript
var classesArray = document.getElementById('myElementID').className.split(/\s+/);
1

function myFunction() {
  var element = document.getElementById("myDIV");

    element.classList.add("mystyle");
} 
0

New to Communities?

Join the community