HTML DOM classList Property Last Updated : 13 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. NOTE: The classList property is not supported in IE9 and earlier. Syntax: const elementClasses = elementNodeReference.classList; Method: add(class1, class2, ...) : Adds one more class to an element. If above class already exist in the element's class attribute they are ignored. remove(class1, class2, ...) : Removes the specified class from element. Class which does not exist does not throw an error. contains(class) : Checks whether the specified class value exists in the element's class attribute. Returns boolean value accordingly. item(index) : This returns the class value by index in the collection of classes. If the index is out of range it returns null. toggle(class, force) : Toggles between a class name for an element. The first parameter removes specified class from an element and returns false. If the class does not exist, it adds class to the element, and the return true. The optional second parameter is a Boolean value that forces the class to be added or removed. When a second parameter is present & evaluates to true, add the specified class value, and if it evaluates to false, it forces to removes the specified class whether it exists or not. Example-1:Adding and removing a class. html <!DOCTYPE html> <html> <head> <title> HTML | DOM classList Property </title> <style> .mystyle { align: center; border: 1px solid black; height: 100px; padding-top: 35px; background: lightgreen; color: Black; font-size: 70px; } </style> </head> <body> <p> Click the buttons to see the add and remove of "mystyle" class to DIV. </p> <button onclick="myFunction()"> Add class </button> <div id="myDIV"> GeeksforGeeks </div> <script> function myFunction() { document.getElementById( "myDIV").classList.add("mystyle"); } function Remove() { document.getElementById( "myDIV").classList.remove("mystyle"); } </script> <button onclick="Remove()">Remove class</button> </body> </html> Output : Before adding a class After clicking on add class button After clicking on remove class button Example-2: Toggling between classes html <!DOCTYPE html> <html> <head> <title> HTML | DOM classList Property </title> <style> .mystyle { align: center; border: 1px solid black; height: 100px; padding-top: 35px; background: lightgreen; color: Black; font-size: 70px; } .newClassName { align: center; border: 1px solid black; height: 50px; padding-top: 35px; background: green; color: white; font-size: 50px; } </style> </head> <body> <p> Click the buttons to see the add and remove of "mystyle" class to DIV. </p> <button onclick="myFunction()"> toggle </button> <div id="myDIV" class="mystyle"> GeeksforGeeks </div> <script> function myFunction() { document.getElementById( "myDIV").classList.toggle("newClassName"); } </script> </body> </html> Output : Before toggle After toggle Example-3: html <!DOCTYPE html> <html> <head> <title> HTML | DOM classList Property </title> <style> .mystyle { width: 500px; height: 50px; } .anotherClass { background-color: lightGreen; } .thirdClass { text-align: center; font-size: 25px; color: black; margin-bottom: 10px; } </style> </head> <body> <div id="myDIV" class="mystyle anotherClass thirdClass"> GeeksforGeeks </div> <button onclick="myFunction()"> click to count the classes </button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById( "myDIV").classList.length; document.getElementById("demo").innerHTML = x; } </script> </body> </html> Output : Before click After click Supported Browser: The browsers supported by DOM classList Property are listed below: Google Chrome 22.0 and aboveEdge 16.0 and aboveInternet Explorer 10.0 and aboveFirefox 3.6 and aboveOpera 11.5 and aboveSafari 7.0 and above Comment More infoAdvertise with us Next Article HTML DOM Style display Property A ashishsaini3 Follow Improve Article Tags : Web Technologies HTML HTML-DOM Similar Reads HTML DOM className Property In the HTML document, the className property is used to set or return the value of an elementâs class attribute. Using this property, the user can change the class of an element to the desired class. Syntax: returns the className propertyHTMLElementObject.className;sets the className propertyHTMLEle 3 min read SVG Element.classList Property The SVG Element.classList property returns the classList of the given element. Syntax: var attr = element.classList Return value: This property returns classList of the element. Example 1: HTML <!DOCTYPE html> <html> <body> <svg width="350" height="350" xmlns 1 min read HTML DOM Style display Property The HTML DOM Style display property is used to set or return the display type of an element. It is similar to the visibility property, which displays or hides the element. With a slight difference in display: none, hiding the entire element, while visibility: hidden meaning only the contents of the 3 min read HTML | DOM Ol type Property The DOM Ol type Property is used to set or return the type attribute in an ordered list. This attribute defines which type(1, A, a, I and i) of order you want in your list numeric, alphabetic or roman numbers. Syntax: It is used to return the type property. olObject.type It is used to set the type p 2 min read CSS list-style Property The list-style property in CSS is used to set the list style. the CSS list-style property is a shorthand for setting the list-style-type, list-style-position, and list-style-image properties in one declaration. It defines the appearance of list item markers, including their type (e.g., disc, circle) 3 min read CSS list-style-position Property CSS List-Style-Position Property is a key attribute that determines the position of the marker box about the principal block box. This property gives you the control to fine-tune the placement of your list markers, enhancing the structure and readability of your lists.Syntax:list-style-position: out 2 min read Like