What is classlist in javascript?

Software
AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Listen

Introduction

In JavaScript, the classList is a property that allows developers to work with the classes of an HTML element. It provides a set of methods to manipulate and interact with the classes assigned to an element. This article will explore the classList in JavaScript and its various use cases.

Working with classList

The classList property is available on all HTML elements and provides several methods to work with classes. Some of the commonly used methods include:

add(className1, className2, …): This method adds one or more classes to the element. Multiple class names can be passed as arguments, separated by commas.

remove(className1, className2, …): This method removes one or more classes from the element. Similar to the add method, multiple class names can be passed as arguments.

toggle(className, force): The toggle method adds a class if it doesn’t exist on the element, and removes it if it already exists. It also accepts an optional second argument, force, which can be used to explicitly add or remove a class based on a boolean value.

contains(className): This method checks if the element has a specific class. It returns true if the class exists, and false otherwise.

replace(oldClass, newClass): The replace method replaces a class with another class on the element.

Example Usage

Let’s consider a simple example to understand how the classList property works. Suppose we have an HTML element with the class “box”. We can use the classList property to manipulate this class dynamically:

“`javascript
const element = document.getElementById(“myElement”);

// Add a class
element.classList.add(“highlight”);

// Remove a class
element.classList.remove(“box”);

// Toggle a class
element.classList.toggle(“active”);

// Check if a class exists
const hasClass = element.classList.contains(“highlight”);

// Replace a class
element.classList.replace(“highlight”, “newClass”);
“`

In the example above, we add the “highlight” class to the element, remove the “box” class, toggle the “active” class, check if the “highlight” class exists, and finally replace the “highlight” class with “newClass”.

Browser Compatibility

The classList property is supported by all modern browsers, including Chrome, Firefox, Safari, and Edge. However, it is not supported in Internet Explorer 9 and below. To work around this limitation, you can use alternative methods like manually manipulating the className property or using a polyfill library.

Conclusion

The classList property in JavaScript provides a convenient way to work with classes of HTML elements. It offers methods to add, remove, toggle, check, and replace classes on an element. By utilizing the classList property, developers can dynamically modify the appearance and behavior of elements based on user interactions or other conditions.

References

– developer.mozilla.org
– www.w3schools.com