• This property uses “classList.length” property which returns the class names of the element in the form of DOMTokenlist(set of space-separated tokens).
  • Any DOM element will have a classList object which contains a collection of methods that let you manage the classes, such as adding/removing.
  • In this tutorial, you will learn how to modify CSS classes using the JavaScript classList object for your DOM Manipulation project.
  • // use the classList API to remove and add classes div.classList.remove("foo"); div.classList.add("anotherclass"); View another examples.
  • Definition and Usage. The classList property returns the class name(s) of an element, as a DOMTokenList object.
  • create element and append class const div = document.createElement('div'); div.classList.add('class-1', 'class-2', 'class-3', 'class-n')
  • In this article, we would like to show you how to remove all items from the element's classList using JavaScript.
  • Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className.
  • export default MyComponent; In this example, we use the classList property to toggle the "highlight" class on the element with the ID "result."
  • el.addEventListener('mouseout', function(){ el.classList.remove("hover"); el.classList.add("nature") ... Adding a class to a list element.