Sometimes, we want to add and remove multiple CSS classes to an element with JavaScript.
In this article, we’ll look at how to add and remove multiple CSS classes to an element with JavaScript.
How to add and remove multiple CSS classes to an element with JavaScript?
To add and remove multiple CSS classes to an element with JavaScript, we can use the classList.add and classList.remove methods.
For instance, we write
buttonTop.classList.add("myButton", "myStyle");
to call classList.add on the buttonTop element to add the myButton and myStyle classes.
And we write
buttonTop.classList.remove("myElement", "myButton", "myStyle");
to call classList.add on the buttonTop element to remove the myElement, myButton and myStyle classes.
Conclusion
To add and remove multiple CSS classes to an element with JavaScript, we can use the classList.add and classList.remove methods.
