Change an element class JavaScript

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Change an element class with javascript</title>
        <style type="text/css">
            .default{
                background-color: red;
            }
            .changedClass{
                background-color: green;
            }
            #myPara{
                margin-top: 20px;
            }
            #myButton{
                padding: 10px;
            }
            body {
                text-align:center;
            }
            h1 {
                color:green;
            }
        </style>
        <script type="text/javascript">
            function changeClass() {
                document.getElementById('myButton').className = "changedClass";
                var button_class = document.getElementById('myButton').className;
                document.getElementById('myPara').innerHTML = "New class name: " 
                + button_class;
            }
        </script>
    </head>
    <body>
        <h1>GeeksforGeeks</h1>
        <h2>Chenge class name of element</h2>
        <button class="default" onclick="changeClass()" 
                    id="myButton">Click Here!</button><br>
        <p id="myPara">Old class name: default</p>
    </body>
</html>                    

chevron_right


Output:
new class

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>Change an element class with javascript</title>
        <style type="text/css">
            .default{
                background-color: red;
                padding:5px;
                border:1px solid black;
                border-radius:3px;
            }
            .newclass1 {
                color:white;
                font-size:16px;
                font-weight:bold;
                border:1px solid black;
            }
            .newclass2 {
                padding: 10px;
                background-color:green;
                border-radius:3px;
            }
                h1 {
                color:green;
            }
            body {
                text-align:center;
            }
        </style>
        <script type="text/javascript">
            function changeClass() {
                document.getElementById('myButton').className = "newclass1";
                document.getElementById('myButton').classList.add("newclass2");
                var button_class = document.getElementById('myButton').className;
                document.getElementById('myPara').innerHTML = "New class name: "
                + button_class;
            }
        </script>
    </head>
    <body>
        <h1>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>
    </body>
</html>                        

chevron_right


Output:
new class



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.