How to Remove a Class from Multiple Elements using JavaScript?

Estimated read time 1 min read

To remove a class from multiple elements using JavaScript, you can use a loop and the classList.remove() method:

var elems = document.querySelectorAll(".className");
for (var i = 0; i < elems.length; i++) {
  elems[i].classList.remove("className");
}

Here, document.querySelectorAll(".className") selects all elements with the class "className" in the DOM, and the loop removes the class from each element.

Alternatively, you can also use the className property to manipulate the class list:

var elems = document.querySelectorAll(".className");
for (var i = 0; i < elems.length; i++) {
  elems[i].className = elems[i].className.replace(/\bclassName\b/,'');
}

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply