Search results
Results from the WOW.Com Content Network
Syntax. element.classList. Return Value. The classList property is read-only, but you can use the methods listed below, to add, toggle or remove CSS classes from the list: classList Properties and Methods. More Examples. Add multiple classes to the an element: element.classList.add("myStyle", "anotherClass", "thirdClass"); Try it Yourself »
The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element. This can then be used to manipulate the class list. Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.
The classList property ensures that duplicate classes are not unnecessarily added to the element. In order to keep this functionality, if you dislike the longhand versions or jQuery version, I'd suggest adding an addMany function and removeMany to DOMTokenList (the type of classList): var array = classes.split(' ');
You can use the classList.add OR classList.remove method to add/remove a class from a element. var nameElem = document.getElementById("name") nameElem.classList.add("anyclss") The above code will add(and NOT replace) a class "anyclass" to nameElem.
The element’s classList property returns the live collection of CSS classes of the element. Use the add() and remove() methods to add CSS classes to and remove CSS classes from the class list of an element. Use the replace() method to replace an existing class with a new one.
classList add () method explained. The add() method allows you to add one or more class names to an element. You only need to pass the class names you want to add as a comma-delimited string: Here’s an example of adding a new class name to the <div> element above: const div = document.getElementById("header"); div.classList.add("expand");
The classList Property is a read-only property. This property uses “classList.length” property which returns the class names of the element in the form of DOMTokenlist (set of space-separated tokens). However, this property is to use add, remove and toggle CSS classes on an element.