]> source.dussan.org Git - jquery.git/commitdiff
Set the property corresponding to a boolean attribute when setting to true. Fixes...
authortimmywil <tim.willison@thisismedium.com>
Wed, 4 May 2011 19:53:00 +0000 (15:53 -0400)
committertimmywil <tim.willison@thisismedium.com>
Wed, 4 May 2011 19:53:00 +0000 (15:53 -0400)
- Once boolean properties had been modified natively, setting the attribute no longer set the current value

src/attributes.js
test/unit/attributes.js

index 97638632cec5efde5829e3b96771070bda9cb7cb..991c2a2047988e7ab209b8502d203eaa2e97e261 100644 (file)
@@ -301,7 +301,7 @@ jQuery.extend({
                        return jQuery( elem )[ name ]( value );
                }
                
-               var ret, hooks,
+               var ret, hooks, boolProp,
                        notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
                
                // Normalize the name if needed
@@ -326,7 +326,12 @@ jQuery.extend({
                        } else {
 
                                // Set boolean attributes to the same name
+                               // Also set the DOM property
                                if ( value === true && !rspecial.test( name ) ) {
+                                       boolProp = jQuery.propFix[ name ] || name;
+                                       if ( !rinvalidChar.test( boolProp ) && typeof elem[ boolProp ] === "boolean" ) {
+                                               elem[ boolProp ] = true;
+                                       }
                                        value = name.toLowerCase();
                                }
 
@@ -338,7 +343,6 @@ jQuery.extend({
                        return hooks.get( elem, name );
 
                } else {
-                       var boolProp;
 
                        // Align boolean attributes with corresponding properties
                        // Do not check the property if the name contains characters
index 80efed8291a21ad6406cd0342d57b54c8bd901f0..5f35e94824ebd70618fdd849a42d261fef91574a 100644 (file)
@@ -163,7 +163,8 @@ test("attr(String, Object)", function() {
        equals( jQuery("#name").attr("name"), "something", "Set name attribute" );
        jQuery("#name").attr("name", null);
        equals( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
-       jQuery("#check2").attr("checked", true);
+
+       jQuery("#check2").prop("checked", true).prop("checked", false).attr("checked", true);
        equals( document.getElementById("check2").checked, true, "Set checked attribute" );
        equals( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
        equals( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );