Skip to content
Permalink
Browse files

Avoid jQuery(this) and a closure for .toggle(Boolean), close gh-1271.

  • Loading branch information
jbedard authored and dmethvin committed Jun 3, 2013
1 parent 0b7c44a commit e53a91909061c7a7280a274990db179b94db81b6
Showing with 14 additions and 7 deletions.
  1. +10 −5 src/attributes.js
  2. +4 −2 src/css.js
@@ -102,8 +102,11 @@ jQuery.fn.extend({
},

toggleClass: function( value, stateVal ) {
var type = typeof value,
isBool = typeof stateVal === "boolean";
var type = typeof value;

if ( typeof stateVal === "boolean" && type === "string" ) {
return stateVal ? this.addClass( value ) : this.removeClass( value );
}

if ( jQuery.isFunction( value ) ) {
return this.each(function( i ) {
@@ -117,13 +120,15 @@ jQuery.fn.extend({
var className,
i = 0,
self = jQuery( this ),
state = stateVal,
classNames = value.match( core_rnotwhite ) || [];

while ( (className = classNames[ i++ ]) ) {
// check each className given, space separated list
state = isBool ? state : !self.hasClass( className );
self[ state ? "addClass" : "removeClass" ]( className );
if ( self.hasClass( className ) ) {
self.removeClass( className );
} else {
self.addClass( className );
}
}

// Toggle whole class name
@@ -137,10 +137,12 @@ jQuery.fn.extend({
return showHide( this );
},
toggle: function( state ) {
var bool = typeof state === "boolean";
if ( typeof state === "boolean" ) {
return state ? this.show() : this.hide();
}

return this.each(function() {
if ( bool ? state : isHidden( this ) ) {
if ( isHidden( this ) ) {
jQuery( this ).show();
} else {
jQuery( this ).hide();

0 comments on commit e53a919

Please sign in to comment.
You can’t perform that action at this time.