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. |
|||||||||
marked as duplicate by lincolnk, elclanrs Aug 12 '14 at 16:58This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. |
|||||||||
|
You could simply set the elements class to nothing.
or if you wanted to keep a particular class you could just reset the class
|
|||||||||||||||||||||
|
where |
|||||||||||||||||||||
|
The right and standard way to do it is using
I'm going to publish an article about that on http://hacks.mozilla.org next week with fallback mechanism for other browsers. Documentation: https://developer.mozilla.org/en/DOM/element.classList |
|||||||||||||||||||||
|
Here's a way to bake this functionality right into all DOM elements:
|
|||||||||||||||||
|
|
|||||
|
More at https://developer.mozilla.org/en-US/docs/Web/API/element.classList |
|||||
|
Edit Okay, complete re-write. It's been a while, I've learned a bit and the comments have helped.
Old Post I was just working with something like this. Here's a solution I came up with...
Now you can call or chain it: |
|||||||||||||||||
|
try:
|
|||
|
It's very simple, I think.
|
|||||||||||||||||
|
All of these answers are way too complicated, try
The result would be a return of the string
Super easy. Credits go to jondavidjohn Remove portion of string in Javascript |
|||||
|
|
||||
|
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 = "".
|
|||
|
With the plus sign ('+') appending the class as opposed to overwriting any existing classes |
||||
|
There is also Take a look at this jsFiddle example to see it in action. |
|||||||||
|