views:

26

answers:

1

Hi,

I have the following piece of code:

if((String(parent).length > 0) && (String(this.className).length > 0)) {

where this.className contains the value "top currentMenu"

Using this.className name, I need a means of removing from this.className, "currentMenu" ONLY using jQuery, so that the end result for this.className is just "top"

Any help would be great.

Thanks.

+4  A: 

It is simply:

$(this).removeClass('currentMenu')

Reference: .removeClass


In plain JavaScript you can do something like:

function removeClass(node, cls) {
    if(node && node.className && node.className.indexOf(cls) >= 0) {
        var pattern = new RegExp('\\s*' + cls + '\\s*');
        node.className = node.className.replace(pattern, ' ');
    }
}

Or I don't understand your problem :)

Felix Kling
hi felix, tried this but didn't work. I think I need to actually use this.className and then strip out of this the "currentMenu" class name only.
tonsils
@tonsils: You mean you tried `$(this).removeClass()` ? This definitely works, you must be doing something wrong. Can you post more code? In which context are you doing this? Don't forget to pass `this` into `$()` before you call a jQuery function.
Felix Kling
is there anyway achieving your removeClass function with simple jQuery by passing in the second parameter only?
tonsils
@tonsils: `removeClass` is the simple jQuery solution. The rest in my function is just string processing and jQuery won't help you here.
Felix Kling