Answers for "classlist add javascript"

89

js add class

var element = document.getElementById('element');
element.classList.add('class-1');
element.classList.add('class-2', 'class-3');
element.classList.remove('class-3');
Posted by: Guest on May-07-2020
9

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");
Posted by: Guest on July-09-2020
39

js classlist

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.
Posted by: Guest on May-22-2020
3

ClassList

const div = document.createElement('div');
div.className = 'foo';

// our starting state: <div class="foo"></div>
console.log(div.outerHTML);

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");

// <div class="anotherclass"></div>
console.log(div.outerHTML);

// if visible is set remove it, otherwise add it
div.classList.toggle("visible");

// add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10 );

console.log(div.classList.contains("foo"));

// add or remove multiple classes
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");

// add or remove multiple classes using spread syntax
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);

// replace class "foo" with class "bar"
div.classList.replace("foo", "bar");
Posted by: Guest on February-20-2021
2

add to classlist javascript

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");
Posted by: Guest on June-29-2020
3

classlist

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");
Posted by: Guest on March-22-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language