Make sure attr() works on non DOM element nodes. Fixes #7202, #7451, #7500.
[jquery.git] / src / attributes.js
index 4393a9f..f463b4a 100644 (file)
@@ -55,7 +55,9 @@ jQuery.fn.extend({
                                                elem.className = value;
 
                                        } else {
-                                               var className = " " + elem.className + " ", setClass = elem.className;
+                                               var className = " " + elem.className + " ",
+                                                       setClass = elem.className;
+
                                                for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
                                                        if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
                                                                setClass += " " + classNames[c];
@@ -103,7 +105,8 @@ jQuery.fn.extend({
        },
 
        toggleClass: function( value, stateVal ) {
-               var type = typeof value, isBool = typeof stateVal === "boolean";
+               var type = typeof value,
+                       isBool = typeof stateVal === "boolean";
 
                if ( jQuery.isFunction( value ) ) {
                        return this.each(function(i) {
@@ -115,7 +118,9 @@ jQuery.fn.extend({
                return this.each(function() {
                        if ( type === "string" ) {
                                // toggle individual class names
-                               var className, i = 0, self = jQuery(this),
+                               var className,
+                                       i = 0,
+                                       self = jQuery( this ),
                                        state = stateVal,
                                        classNames = value.split( rspaces );
 
@@ -200,7 +205,6 @@ jQuery.fn.extend({
                                if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
                                        return elem.getAttribute("value") === null ? "on" : elem.value;
                                }
-                               
 
                                // Everything else, we just grab the value
                                return (elem.value || "").replace(rreturn, "");
@@ -266,7 +270,7 @@ jQuery.extend({
                height: true,
                offset: true
        },
-               
+
        attr: function( elem, name, value, pass ) {
                // don't set attributes on text and comment nodes
                if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
@@ -295,7 +299,7 @@ jQuery.extend({
                                var parent = elem.parentNode;
                                if ( parent ) {
                                        parent.selectedIndex;
-       
+
                                        // Make sure that it also works with optgroups, see #5701
                                        if ( parent.parentNode ) {
                                                parent.parentNode.selectedIndex;
@@ -369,6 +373,11 @@ jQuery.extend({
                        // Non-existent attributes return null, we normalize to undefined
                        return attr === null ? undefined : attr;
                }
+               // Handle everything which isn't a DOM element node
+               if ( set ) {
+                       elem[ name ] = value;
+               }
+               return elem[ name ];
        }
 });