Add and remove elements classes

Easiest way to add or remove an elements classes using pure JavaScript or jQuery.


Libraries

Code Snippet
// pure JavaScript
document.getElementById("MyElement").className = "MyClass";
document.getElementById("MyElement").className += "MyClass";
document.getElementById("MyElement").classList.add("MyClass");
document.getElementById("MyElement").classList.remove("MyClass");

// jQuery (not recommend)
$( "MyElement" ).removeClass("MyClass");
$( "MyElement" ).addClass("MyClass");


Implementation

Basically it's not recommend to use jQuery for such an easy thing.





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    jQuery



    Informations

    posted on 2016-09-16 19:41:51

    viewed 80 times

    snippet's UUID is 57dc2eded3d7b

    Similar Snippets