Open In App

Change an element class JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

The class name is used as a selector in HTML which helps to give some value to the element attributes. The document.getElementById() method is used to return the element in the document with the “id” attribute and the “className” attribute can be used to change/append the class of the element.

Syntax:  

document.getElementById('myElement').className = "myclass";

Example 1: In this code change the class of the button from “default” to “changedClass” using the onclick event which in turn changes the background color of the button from RED to GREEN.

html




<!DOCTYPE html>
<html>
 
<head>
    <style>
        .default{
            background-color: red;
        }
        .changedClass{
            background-color: green;
        }
    </style>
</head>
 
<body>
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>
        Change class name of element
    </h2>
    <button class="default"
            onclick="changeClass()"
            id="myButton">
      Click Here!
      </button><br>
    <p id="myPara">
        Old class name: default
    </p>
   
    <script>
        function changeClass() {
            document.getElementById('myButton')
                    .className = "changedClass";
            let button_class = document.getElementById('myButton')
                                       .className;
            document.getElementById('myPara')
                    .innerHTML = "New class name: "
                                 + button_class;
        }
    </script>
</body>
 
</html>


Output: 

Change an element class JavaScript

Change an element class JavaScript

Example 2 : In this code changed the class of the button from “default” to “newclass1” and “newclass2” using the onclick event.

html




<!DOCTYPE html>
<html>
 
<head>
    <style>
        .default{
            background-color: red;
        }
        .newclass1 {
            color:white;
        }
        .newclass2 {
            background-color:green;
        }
         
    </style>
</head>
 
<body style="text-align:center">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>Change class name of element</h2>
    <button class="default"
            onclick="changeClass()"
            id="myButton">
      Click Here!
    </button>
    <p id="myPara">Old class name: default</p>
    <script type="text/javascript">
        function changeClass() {
            document.getElementById('myButton')
                    .className = "newclass1";
            document.getElementById('myButton')
                    .classList.add("newclass2");
            let button_class = document.getElementById('myButton')
                                    .className;
            document.getElementById('myPara')
                    .innerHTML = "New class name: "
                                + button_class;
        }
    </script>
</body>
   
</html>


Output: 

Change an element class JavaScript

Change an element class JavaScript



Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads