Sometimes, we want to append a CSS class to an element with JavaScript.
In this article, we’ll look at how to append a CSS class to an element with JavaScript.
How to append a CSS class to an element with JavaScript?
To append a CSS class to an element with JavaScript, we use the classList
methods.
For instance, we write
element.classList.add("my-class-name");
to add the my-class-name
class to the element
with classList.add
.
And we write
element.classList.remove("my-class-name");
to remove the my-class-name
class from the element
with classList.remove
.
Conclusion
To append a CSS class to an element with JavaScript, we use the classList
methods.