JavaScript | Adding a class name to the element

The class name attribute can be used by CSS and JavaScript to perform certain tasks for elements with the specified class name. Adding the class name by using JavaScript can be done in many ways.

  • Using .className property: This property is used to add a class name to the selected element.

    Syntax:

    element.className += "newClass";

    Example: This example uses .className property to add class name.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html> 
    <html
      
    <head
        <title
            JavaScript to add class name
        </title>
      
        <style>
            .addCSS {
                color: green;
                font-size: 25px;
            }
        </style>
    </head
              
    <body style = "text-align:center;"
          
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1
      
        <p id = "p">
            A Computer Science portal for Geeks.
        </p>
          
        <button onclick = "addClass()"
            AddClass
        </button
          
        <!-- Script to add class name -->        
        <script
            function addClass() {
                var v = document.getElementById("p");
                v.className += "addCSS";
            }
        </script
    </body
      
    </html>                    

    chevron_right

    
    

    Output:

    • Before clicking the button:
    • After clicking the button:
  • Using .add() method: This method is used to add a class name to the selected element.

    Syntax:

    element.classList.add("newClass");

    Example: This example uses .add() method to add class name.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html> 
    <html
      
    <head
        <title
            JavaScript to add class name
        </title>
          
        <!-- Style add to new class -->
        <style>
            .addCSS {
                background-color:green;
                color: white;
                padding:20px;
                font-size: 25px;
            }
        </style>
    </head>
              
    <body style = "text-align:center;"
          
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1
      
        <p id = "p">
            A Computer Science portal for Geeks.
        </p>
      
        <button onclick = "addClass()"
            AddClass
        </button
          
        <!-- Script to add class name -->        
        <script
            function addClass( {
                var v = document.getElementById("p");
                v.classList.add("addCSS");
            }
        </script
    </body
      
    </html>                    

    chevron_right

    
    

    Output:

    • Before clicking the button:
    • After clicking the button:


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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.