How to change class

Learn how to use JavaScript to change the class name of an element.

This is a DIV.

Change class

Change the class of the <div> element from "mystyle" change to "newone":

<div id="myDIV" class="mystyle">
This is a DIV element.
</div>
<script>
function myFunction() {
  const element = document.getElementById("myDIV");  // Get the DIV element
  element.classList.remove("mystyle"); // Remove the mystyle class from the DIV
  element.classList.add("newone"); // Add the newone class to the DIV
}
</script>

try it yourself

related page

Tutorial:How to Switch Class

Reference Manual:HTML DOM Element classList Property