Bug Tracker

Opened 14 years ago

Closed 13 years ago

Last modified 8 years ago

#172 closed enhancement (fixed)

removeClass() with multiple classes

Reported by: john Owned by:
Priority: trivial Milestone: 1.1a
Component: core Version: 1.1a
Keywords: Cc:
Blocked by: Blocking:

Description

.removeClass() and .toggleClass() multiple classes.

Change History (4)

comment:1 Changed 13 years ago by joern

You can add multiple classes by using addClass("foo bar"), therefore removeClass should work similar, eg. removeClass("bar foo")

removeClass: function(c){
  c = c.split(' ');
  if(c.length > 1)
    for(var i=0, s; s = c[i]; i++)
      jQuery.className.remove(this, s);
  else
    jQuery.className.remove(this, c[0]);
}

toggleClass would work similar:

toggleClass: function( c ){
  var checkClass = function(element, c) {
    jQuery.className[ jQuery.className.has(element,c) ? "remove" : "add" ](element,c);
  };
  c = c.split(' ');
  if(c.length > 1)
    for(var i=0, s; s = c[i]; i++)
      checkClass(this, s);
  else
    checkClass(this, c[0]);
}

Is there a bette way to implement those? Maybe add stuff to jQuery.className that handles multiple names and can be used by both remove and toggle?

comment:2 Changed 13 years ago by dave.methvin

Back when removeClass used a regexp this came for free because you could say .removeClass("(class1|class2)") to remove class1 and class2. I guess the split/splice approach was benchmarked as being a lot faster?

comment:3 Changed 13 years ago by john

Type: bugenhancement

This was never the intended functionality of .addClass()/.removeClass()/.toggleClass(). Adding multiple classes using addClass should be deemed unexpected, and undocumented, until otherwise noted.

comment:4 Changed 13 years ago by john

Milestone: 1.1
Resolution: fixed
Status: newclosed
Version: 1.1

Resolved in SVN.

Note: See TracTickets for help on using tickets.