Skip to main content

Bug Tracker

Side navigation

#172 closed enhancement (fixed)

Opened September 02, 2006 09:50AM UTC

Closed January 02, 2007 05:53PM UTC

Last modified March 15, 2012 12:30AM UTC

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.

Attachments (0)
Change History (4)

Changed October 27, 2006 02:48PM UTC by joern comment:1

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?

Changed October 30, 2006 10:18PM UTC by dave.methvin comment:2

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?

Changed November 17, 2006 08:21PM UTC by john comment:3

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.

Changed January 02, 2007 05:53PM UTC by john comment:4

milestone: → 1.1
resolution: → fixed
status: newclosed
version: → 1.1

Resolved in SVN.