Modify ↓
Ticket #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: | ||
| Blocking: | Blocked by: |
Description
.removeClass() and .toggleClass() multiple classes.
Change History
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?
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

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?