Open In App

HTML DOM classList Property

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: 



Example-1:Adding and removing a class. 
 




<!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 : 
 

Example-2: Toggling between classes  




<!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 : 
 

Example-3:  




<!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 : 
 

Supported Browser: The browsers supported by DOM classList Property are listed below:  


Article Tags :