Introduction
In JavaScript, we may wish to change an element's class. In this post, we'll look at how to do it.
How to change an element's class with JavaScript
To change an element's class with JavaScript you can use the classList
property that provides methods for classes manipulation:
To change class in JavaScript we can also use className
property
Set the className property to replace all current classes with one or more new classes:
To add a my-class
class to an element, use the following code:
To remove a my-class
class from an element, use the following code:
To check if an element contains a specific class:
Conclusion
In this short article, we described how to change an element's class with JavaScript.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}