Bug Tracker

Modify

Ticket #172 (closed enhancement: fixed)

Opened 7 years ago

Last modified 14 months ago

removeClass() with multiple classes

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

Description

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

Change History

comment:1 Changed 7 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 7 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 7 years ago by john

  • Type changed from bug to enhancement

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 6 years ago by john

  • Status changed from new to closed
  • Version set to 1.1
  • Resolution set to fixed
  • Milestone set to 1.1

Resolved in SVN.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.