Answers for "set class to element javascript"

89

js set class

var element = document.getElementById('element');
element.classList.add('class-1');
element.classList.add('class-2', 'class-3');
element.classList.remove('class-3');
Posted by: Guest on May-07-2020
1

javascript give class to element

element.classList.add("div-class");
Posted by: Guest on February-01-2021
31

js add class

var someElement= document.getElementById("myElement");
    someElement.className += " newclass";//add "newclass" to element (space in front is important)
Posted by: Guest on July-24-2019
3

change class javascript

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

<html> 
    <head> 
        <title>Change class of an element with javascript</title> 
        <style type="text/css"> 
            .redbutton{ 
                background-color: red; 
            } 
            .greenbutton{ 
                background-color: green; 
            } 
        </style> 
        <script type="text/javascript"> 
            function changeClass() { 
                document.getElementById('clickme').className = "greenbutton"; 
                var new_class = document.getElementById('clickme').className; 
                document.getElementById('classChange').innerHTML = "New Class Name : "  
                + new_class; 
            } 
        </script> 
    </head> 
    <body> 
        <button class="redbutton" id="clickme" onclick="changeClass()" 
                    >Change Class</button><br> 
        <p id="classChange">Class Name: redbutton</p> 
    </body> 
</html>
Posted by: Guest on April-21-2020

Code answers related to "set class to element javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language