How to delete a class

Learn how to remove a class from an element using JavaScript.

Click the button to remove a class from me!

Delete class

Step 1 - Add HTML:

In this example, we will use a button to remove a class from id="myDIV" from the <div> element "mystyle" Class:

<button onclick="myFunction()">Try it</button>
<div id="myDIV" class="mystyle">
  This is a DIV element.
</div>

Step 2 - Add CSS:

Set the style for the specified class name:

.mystyle {
  width: 100%;
  padding: 25px;
  background-color: coral;
  color: white;
  font-size: 25px;
}

Step 3 - Add JavaScript:

Get id="myDIV" from the <div> element, and remove "mystyle" Class:

function myFunction() {
  var element = document.getElementById("myDIV");
  element.classList.remove("mystyle");
}

Try it yourself

Related pages

Tutorial:How to switch class

Tutorial:How to add class

Reference manual:The classList property of the HTML DOM Element

Reference manual:The className property of the HTML DOM Element