Answers for "javascript change class"

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
10

Javascript change element class

document.getElementById("myElementID").classList.add('myClassName');
document.getElementById("myElementID").classList.remove('myClassName');
Posted by: Guest on July-22-2019
1

js document.getelementsbyclassname modify innertext

<body>

<p class="demo">JavaScript can change the content of an HTML element.</p>    
<p class="demo">Yolo</p>   

<button type="button" onclick="myFunction()">Click Me!</button>

<script>        
function myFunction()
{
x=document.getElementsByClassName("demo");  // Find the elements
    for(var i = 0; i < x.length; i++){
    x[i].innerText="Hello JavaScript!";    // Change the content
    }

}

</script>
</body>
Posted by: Guest on March-25-2020
8

change elements class javascript

document.getElementById("MyElement").classList.add('MyClass');

document.getElementById("MyElement").classList.remove('MyClass');

if ( document.getElementById("MyElement").classList.contains('MyClass') )
{
document.getElementById("MyElement").classList.toggle('MyClass');
}
Posted by: Guest on July-06-2020
12

javascript change element class

//Overwrite all classes
element.className = "class";

//Add class
element.classList.add("class");
Posted by: Guest on June-02-2020
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 "javascript change class"

Code answers related to "Javascript"

Browse Popular Code Answers by Language