Sometimes, we want to remove a class from elements in pure JavaScript.
In this article, we’ll look at how to remove a class from elements in pure JavaScript.
How to remove a class from elements in pure JavaScript?
To remove a class from elements in pure JavaScript, we use the classList.remove method.
For instance, we write
Array.from(document.querySelectorAll(".widget.hover")).forEach((el) => {
el.classList.remove("hover");
});
to select all elements with the widget and hover classes with querySelectorAll.
Then we call Array.from on the returned node list to convert it to an array.
Next, we call forEach with a callback that calls classList.remove to remove the hover class from each element el.
Conclusion
To remove a class from elements in pure JavaScript, we use the classList.remove method.