Last active
September 3, 2019 23:00
-
-
Save masautt/c3f5db7e027591343eef39046a4215cf to your computer and use it in GitHub Desktop.
How to change element's class in JavaScript?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Change the class altogether? | |
| document.getElementById("MyElement").className = "MyClass"; | |
| // Add to the class? | |
| document.getElementById("MyElement").className += "MyClass"; | |
| // Add or Remove a new class? | |
| document.getElementById("MyElement").classList.add('MyClass'); | |
| document.getElementById("MyElement").classList.remove('MyClass'); | |
| // Check if an element has a class? | |
| if ( document.getElementById("MyElement").classList.contains('MyClass') ) | |
| // Toggle a class on an element? | |
| document.getElementById("MyElement").classList.toggle('MyClass'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment