make sure toggleClass does not delete classNames when forcefully removing classes...
[jquery.git] / src / attributes.js
index 22275ad..f3e1b4c 100644 (file)
@@ -46,7 +46,7 @@ jQuery.fn.extend({
                        if ( elem ) {
                                if( jQuery.nodeName( elem, 'option' ) )
                                        return (elem.attributes.value || {}).specified ? elem.value : elem.text;
-                               
+
                                // We need to handle select boxes special
                                if ( jQuery.nodeName( elem, "select" ) ) {
                                        var index = elem.selectedIndex,
@@ -75,7 +75,7 @@ jQuery.fn.extend({
                                                }
                                        }
 
-                                       return values;                          
+                                       return values;
                                }
 
                                // Everything else, we just grab the value
@@ -130,9 +130,24 @@ jQuery.each({
        },
 
        toggleClass: function( classNames, state ) {
-               if( typeof state !== "boolean" )
-                       state = !jQuery.className.has( this, classNames );
-               jQuery.className[ state ? "add" : "remove" ]( this, classNames );
+               var type = typeof classNames;
+               if ( type === "string" ) {
+                       // toggle individual class names
+                       var isBool = typeof state === "boolean", className, i = 0,
+                               classNames = classNames.split( /\s+/ );
+                       while ( (className = classNames[ i++ ]) ) {
+                               // check each className given, space seperated list
+                               state = isBool ? state : !jQuery.className.has( this, className );
+                               jQuery.className[ state ? "add" : "remove" ]( this, className );
+                       }
+               } else if ( type === "undefined" || type === "boolean" ) {
+                       if ( this.className ) {
+                               // store className if set
+                               jQuery.data( this, "__className__", this.className );
+                       }
+                       // toggle whole className
+                       this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
+               }
        }
 }, function(name, fn){
        jQuery.fn[ name ] = function(){