toggleClass: function( value, stateVal ) {
var type = typeof value,
- classNames = type === "string" ? value.match( rnotwhite ) : "",
- checker = typeof stateVal === "boolean" ?
- function() { return !stateVal; } :
- jQuery.fn.hasClass;
+ classNames = type === "string" ? value.match( rnotwhite ) : [];
return this.each( function( i ) {
var className,
// Toggle individual class names based on presence or stateVal
while ( ( className = classNames[ c++ ] ) ) {
- if ( checker.call( self, className ) ) {
+ if ( stateVal === false || stateVal !== true && self.hasClass( className ) ) {
self.removeClass( className );
} else {
self.addClass( className );
} );
var testToggleClass = function( valueObj, assert ) {
- assert.expect( 9 );
+ assert.expect( 11 );
var e = jQuery( "#firstp" );
assert.ok( !e.is( ".test" ), "Assert class not present" );
// class name with a boolean
e.toggleClass( valueObj( "test" ), false );
assert.ok( !e.is( ".test" ), "Assert class not present" );
+ e.toggleClass( valueObj( "test" ), false );
+ assert.ok( !e.is( ".test" ), "Assert class still not present" );
e.toggleClass( valueObj( "test" ), true );
assert.ok( e.is( ".test" ), "Assert class present" );
+ e.toggleClass( valueObj( "test" ), true );
+ assert.ok( e.is( ".test" ), "Assert class still present" );
e.toggleClass( valueObj( "test" ), false );
assert.ok( !e.is( ".test" ), "Assert class not present" );