How to Remove a Class from an Element if it Exists using JavaScript?

Estimated read time 1 min read

To remove a class from an element if it exists using JavaScript, you can check if the class is present using the classList.contains() method, and then remove it using the classList.remove() method:

if (element.classList.contains("className")) {
  element.classList.remove("className");
}

Here, element refers to the DOM element, and classList.contains("className") checks if the class "className" is present. If it is, classList.remove("className") removes it.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply