Skip to main content

Bug Tracker

Side navigation

#1077 closed enhancement (wontfix)

Opened March 27, 2007 10:28AM UTC

Closed May 20, 2007 05:24PM UTC

Last modified January 14, 2009 09:14AM UTC

Add an on/off parameter to toggleClass()

Reported by: arrix Owned by:
Priority: major Milestone: 1.2
Component: core Version: 1.1.2
Keywords: Cc:
Blocked by: Blocking:
Description

Please consider adding an on/off parameter to jQuery.fn.toggleClass() so that we can write

$('#button-signIn').toggleClass('disabled', hasSignedIn);

instead of

$('#button-signIn')[hasSignedIn ? 'addClass' : 'removeClass']('disabled');

I think adding/removing a class according to some state is a frequent need.

Index: E:/zm/jquery/jquery/src/jquery/jquery.js
===================================================================
--- E:/zm/jquery/jquery/src/jquery/jquery.js	(revision 1598)
+++ E:/zm/jquery/jquery/src/jquery/jquery.js	(working copy)
@@ -2141,8 +2141,8 @@
 	removeClass: function(c){
 		jQuery.className.remove(this,c);
 	},
-	toggleClass: function( c ){
-		jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
+	toggleClass: function( c, on){
+		jQuery.className[ (on == undefined ? jQuery.className.has(this,c) : !on) ? "remove" : "add" ](this, c);
 	},
 	remove: function(a){
 		if ( !a || jQuery.filter( a, [this] ).r.length )

Attachments (0)
Change History (3)

Changed April 27, 2007 03:17AM UTC by brandon comment:1

I think this could be useful but at first it doesn't read very clear.

Changed May 20, 2007 05:24PM UTC by john comment:2

description: Please consider adding an on/off parameter to jQuery.fn.toggleClass() so that we can write\ {{{\ $('#button-signIn').toggleClass('disabled', hasSignedIn);\ }}}\ \ instead of\ \ {{{\ $('#button-signIn')[hasSignedIn ? 'addClass' : 'removeClass']('disabled');\ }}}\ \ I think adding/removing a class according to some state is a frequent need.\ \ {{{\ Index: E:/zm/jquery/jquery/src/jquery/jquery.js\ ===================================================================\ --- E:/zm/jquery/jquery/src/jquery/jquery.js (revision 1598)\ +++ E:/zm/jquery/jquery/src/jquery/jquery.js (working copy)\ @@ -2141,8 +2141,8 @@\ removeClass: function(c){\ jQuery.className.remove(this,c);\ },\ - toggleClass: function( c ){\ - jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);\ + toggleClass: function( c, on){\ + jQuery.className[ (on == undefined ? jQuery.className.has(this,c) : !on) ? "remove" : "add" ](this, c);\ },\ remove: function(a){\ if ( !a || jQuery.filter( a, [this] ).r.length )\ \ }}}\ Please consider adding an on/off parameter to jQuery.fn.toggleClass() so that we can write \ {{{ \ $('#button-signIn').toggleClass('disabled', hasSignedIn); \ }}} \ \ instead of \ \ {{{ \ $('#button-signIn')[hasSignedIn ? 'addClass' : 'removeClass']('disabled'); \ }}} \ \ I think adding/removing a class according to some state is a frequent need. \ \ {{{ \ Index: E:/zm/jquery/jquery/src/jquery/jquery.js \ =================================================================== \ --- E:/zm/jquery/jquery/src/jquery/jquery.js (revision 1598) \ +++ E:/zm/jquery/jquery/src/jquery/jquery.js (working copy) \ @@ -2141,8 +2141,8 @@ \ removeClass: function(c){ \ jQuery.className.remove(this,c); \ }, \ - toggleClass: function( c ){ \ - jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c); \ + toggleClass: function( c, on){ \ + jQuery.className[ (on == undefined ? jQuery.className.has(this,c) : !on) ? "remove" : "add" ](this, c); \ }, \ remove: function(a){ \ if ( !a || jQuery.filter( a, [this] ).r.length ) \ \ }}} \
resolution: → wontfix
status: newclosed

We don't plan on implementing this feature in core - feel free to create a plugin for it.

Changed January 14, 2009 09:14AM UTC by arrix comment:3

This has been added in [6000]