• 971-553421531
  • info@efaschool.com
  • Dubai(United Arab Emirates)

Master the classList Property in JavaScript

Master the classList Property in JavaScript

What is classList in JavaScript?

The clasList property in JavaScript is a read-only property of the element interface that returns live DOMTokenList object representing the class attributes of the element

The clasList property is a convenient alternative to accessing an element’s list of classes as a space delimited string via the className property.

The clasList property has the following methods:

  • add( ): adds one or more classes to the element
  • remove( ): removes one or more classes from the element.
  • toggle( ): toggles the presence of a class on the element.
  • contains( ): returns a boolean value indicating whether the element has a specified class
  • item( ): return the class at the specified index.
  • length( ): returns the number of classes on the element.

How to use classList in JavaScript?

To use the classList property, you first need to get the classList object of the element you want to work with. You can do this using the classList property.

JavaScript

          
            const element = document.querySelector('element');
            const classList = element.classList;
          
        

Once you have the classList object, you can use its methods to add, remove, toggle, and check for classes on the element.

For example, the following code adds the my-class class to the element.

clasList.add("my-class");

The following code removes the my-class from the element.

classList.remove('my-class');

The following code toggles the presence of the my-class on the element.

classList.toggle('my-class');

The following code checks whether or not the element has the my-class class.

const hasClass = classList.contains("my-class");

The following code returns the class at the specified index

const className = classList.item(0);

The following code returns the number of classes on the element.

const numberOfClasses = classList.length;

Examples of classList in JavaScript

Here are some Examples of how to use the classList property in JavaScript.

  • Add the active class to the element, when the user clicks on it.
  • Add a Class to an Element with JavaScript

              
                const element = document.querySelector("element");
                element.addEventListener("click", function(){
                  element.classList.add("active");
                });
              
            

  • Remove the active class from the element when the user clicks on another element.
  • Remove a class from an element with JavaScript

              
                const element = document.querySelector("element");
                element.element.addEventListener("click", function(){
                  element.classList.remove("active");
                });
              
            

    Toggle a class on the element with JavaScript

              
                const element = document.querySelector("element");
                element.addEventListener("click", function(){
                  element.classList.toggle("active");
                });
              
            

  • check if element has active class
  • check for active class on element in javascript

              
                const element = document.querySelector("element");
                element.addEventListener("click", function(){
                  cosnt hasClass = element.classList.contains("active");
                });
              
            

  • Get the class at the specified index:
  • get class at specified index in javascript

              
                const element = document.querySelector("element");
                element.addEventListener("click", function(){
                  cosnt className = element.classList.item(0);
                });
              
            

  • Get the number of classes on the element:
  • get class at specified index in javascript

              
                const element = document.querySelector("element");
                element.addEventListener("click", function(){
                  cosnt numberOfClasses = element.classList.length;
                });
              
            

    Leave a Reply

    Your email address will not be published. Required fields are marked *