tags:

views:

73

answers:

4

I'm having some strange issues with .removeClass() and .addClass() in jQuery.

Specifically it seems that when I use .removeClass() the class is indeed removed, but a single space is left in it's place. then when I .addClass("secondclass") I get class=" secondclass" (with the space in front).

I'm using jQuery 1.4.1

Is this intended behaviour or a bug? How to stop it?

UPDATE:

Some people have asked what problems this is causing. Well, I'm using thins:

$("img.someclass").click(function() {

I'm testing this with Firefox 3.6.3

Which does not trigger with the space in front of the class name. When I manually remove the space it works fine. It seems that there is more too this problem than the space issue, and most likely the space wasn't causing any issues (glad it's gone though ;) - Will post seperate question regarding the ongoing issue.

+7  A: 

Because multiple classes are delimeted by spaces:

<div class="class1 class2 class3">...</div>

It's easier to add a space rather than code in special conditions. In the above you can simply replace "class3" with "" if you want to remove it, changing it to:

<div class="class1 class2 ">...</div>

Otherwise you need to worry about whether it's the first class or not as to whether to leave a class there. Plus your markup may be putting multiple spaces there (deliberately or inadvertently) anyway.

cletus
I know classes are seperated by spaces, but a space in front of a single class is unusual. Surely it wouldn't take much code for jquery to check for this case. If it's not a bug it's a bit sloppy.
UpTheCreek
@Sosh it's not invalid so you'd be adding extra processing for the case when someone uses Firebug to inspect the result. Not exactly a high priority use case.
cletus
@cletus, yeah I guess so. Anyway, it turned out not to be the cause of the underlying problems. Thanks
UpTheCreek
+1  A: 

@cletus explained it but you can use JQuery's $.trim function to remove the spaces too although I am not sure why you want to do that:

$('selector').attr('class', $.trim($('selector').attr('class'));

Update:

You can do like this:

var class = $('selector').attr('class', $.trim($('selector').attr('class'));

$("img." + class).click(function() {
  // your code......
});
Sarfraz
The space is causing me issues - see the update. I'll take a look at this as an alternative. Thanks.
UpTheCreek
@Sosh: See my updated answer please.
Sarfraz
+1  A: 

It's because jQuery is a poorly coded library and shouldn't be used anyway. Here, I've written another pair that actually works:

function addClass(node, name) {
  node.className += (node.className ? ' ' : '') + name;
}
function removeClass(node, name) {
  node.className = node.className.
    split(' ' + name).join('').
    split(name).join('');
}
Delan Azabani
The issue isn't that they don't work. The issue is the phantom space when there are no classes. Not sure why you shouldn't use jQuery if it is appropriate, I've never had any problems with it to be honest.
Geoff Adams
Hang on... you're saying that the above code in my answer *doesn't* work? It works, and better than the jQuery code for OP's purpose - it only inserts a leading when there are others there. It also removes both with/without space - meaning no spaces are left behind.
Delan Azabani
No, I'm not saying your code doesn't work. I have no idea where you got that idea from.
Geoff Adams
+2  A: 

this is the bug in jQuery version 1.4.1, but this is fixed in jQuery version 1.4.2. If you want to stop this behavior, you can use the latest version.

Elangovan
Updated to 1.4.2, and can confirm the space has gone. Thanks.
UpTheCreek