how to remove a class from an element in javascript

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:

<!-- HTML code -->
<div id="myDiv" class="myClass1 myClass2"></div>
68 chars
3 lines
index.tsx
// JavaScript code
const element = document.getElementById("myDiv");
element.classList.remove("myClass1");
107 chars
4 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