To remove a class from an element in JavaScript, you can use the classList
property of the element. The classList
property returns a collection of the class attributes of the element. To remove a particular class, use the remove
method of the classList
property.
Here's an example that demonstrates how to remove a class from an element using JavaScript:
68 chars3 lines
index.tsx107 chars4 lines
In the above code snippet, we get the element using document.getElementById
and store it in a variable called element
. We then call the classList.remove
method on the element
and pass the name of the class that we want to remove, which is "myClass1"
in this example.
After executing this code, the "myClass1"
class will be removed from the class
attribute of the div
element, and its class
attribute will become "myClass2"
.
gistlibby LogSnag