Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery removeClass() not removing all classes [duplicate]

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?

like image 593
mises Avatar asked May 03 '13 09:05

mises


People also ask

What is removeClass in jQuery?

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.

Which menu is used to remove the class?

removeClass() method removes the specified classes to the matched elements while animating all style changes.

How do you remove a class in CSS?

To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method.


1 Answers

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()

like image 193
Ben Mills Avatar answered Oct 26 '22 08:10

Ben Mills