How TO - Change a Class
Learn how to change the class name of an element with JavaScript.
This is a DIV.
Change Class
Change the class of a <div> element from "mystyle" to "newone":
Example
<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 mystyle class from DIV
element.classList.add("newone"); // Add newone class to DIV
}
</script>
Try it Yourself »
Tip: Also see How To Toggle A Class.
Tip: Learn more about the classList property in our JavaScript Reference.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.