This question already has an answer here:
Could anyone let me know how to remove a class on an element using JavaScript only? Please do not give me an answer with jQuery as I can't use it, and I don't know anything about it.
This question already has an answer here:
Could anyone let me know how to remove a class on an element using JavaScript only? Please do not give me an answer with jQuery as I can't use it, and I don't know anything about it.
More at https://developer.mozilla.org/en-US/docs/Web/API/element.classList
The right and standard way to do it is using
classList
. It is now widely supported in the latest version of most modern browsers:Documentation: https://developer.mozilla.org/en/DOM/element.classList
Here's a way to bake this functionality right into all DOM elements:
There is also
$.toggleClass
,$.addClass
, and$.removeClass
. For documentation, take a look at http://api.jquery.com/toggleClass/.Take a look at this jsFiddle example to see it in action.
I use this JS snippet code :
First of all, I reach all the classes then according to index of my target class, I set className = "".