Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd issue with jQuery .removeClass() not doing anything

I have some code that adds classes to an element and then tries to remove them and add different ones 1 second later. I'm getting some very odd behavior that I can't even reproduce in a simple jsfiddle example.

Here's the relevant JavaScript code I have:

console.log('before destroyed: ' + currentTile.get(0).className);
currentTile.addClass(classes.destroyed);
console.log('after destroyed: ' + currentTile.get(0).className);

setTimeout(function () {
    console.log('before blanking: ' + currentTile.get(0).className);
    currentTile.removeClass().addClass(classes.blank + ' ui-draggable');
    console.log('after blanking: ' + currentTile.get(0).className);
}, 2000);

And here is what the console is saying:

enter image description here

As you can see, adding the destroyed class works fine, but the call to removeClass() inside of the setTimeout appears to be doing nothing, and then the .addClass(classes.blank + ' ui-draggable'); also appears to be working fine. Also, if I pass a single class to removeClass it removes that one class without a problem.

If it were an issue of context or currentTile being the wrong element, I would think the addClass would also fail? Anyone have any idea what is going on here?

Additional info: jQuery latest (v.1.9.0 I think), jQuery UI v 1.10.0, Chrome v.24.0.1312.56 m


Edit: The problem appears to be directly related to jQuery UI, and can be seen happening in this fiddle.


Edit 2: This was confirmed as a bug in jQuery, and has been fixed.

like image 250
jbabey Avatar asked Jan 26 '13 00:01

jbabey


People also ask

How to removeClass using jQuery?

jQuery removeClass() MethodThe 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.

How to replace classes in jQuery?

In jQuery, to replace one class properties with another, there is a function called replaceClass() through which it takes in two different class names the first class name is specified as a class which should be replaced with the second class name that is specified as the second parameter in the function, where it ...

How to remove div class in jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.

Has class add and remove jQuery?

jQuery Manipulating CSSaddClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements. css() - Sets or returns the style attribute.


1 Answers

Try using .removeAttr('class') rather than .removeClass().

DEMO:

http://jsfiddle.net/MvvmJ/8/

Hope this helps and let me know if you have any questions!

like image 147
Dom Avatar answered Sep 16 '22 16:09

Dom