I have an element like this:
<div class="one two three" id="waterhorse">horse</div>
When I run this code in the browser console:
$("#waterhorse").removeClass();
I get this:
[<div id="waterhorse" class="one two three">horse</div>]
In other words, it doesn't work; it doesn't remove any classes on the element. I unfortunately can't reproduce it in jsfiddle.
However, I can remove a specific class:
$("#waterhorse").removeClass("two");
Also, this will remove all classes:
$("#waterhorse").removeAttr("class");
Any idea why the latter works to remove all classes, but the former doesn't?
The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
removeClass() method removes the specified classes to the matched elements while animating all style changes.
To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method.
Seems like this is a known problem with jQuery and jQueryUI not playing nicely together:
Odd issue with jQuery .removeClass() not doing anything
http://bugs.jqueryui.com/ticket/9015
The answer above is a workaround that uses .removeAttr('class')
instead of .removeClass()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With