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.
|
You could simply set the elements class to nothing.
or if you wanted to keep a particular class you could just reset the class
|
|||||||||||||||||||||
|
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 = "".
|
|||
|
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: |
|||||||||||||||||
|
With the plus sign ('+') appending the class as opposed to overwriting any existing classes |
||||
|
|
||||
|
Actually, the right way (standard) to do (but only works with Firefox3.6):
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 |
|||||||||||||||||||||
|
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 |
|||||
|
More at https://developer.mozilla.org/en-US/docs/Web/API/element.classList |
|||
|
where |
|||||||||||||||||||||
|
There is also Take a look at this jsFiddle example to see it in action. |
|||||||||
|
Here's a way to bake this functionality right into all DOM elements:
|
|||||||||||||
|
try:
|
|||
|
|
|||
|