]> source.dussan.org Git - jquery.git/commitdiff
Make .attr(name, null) equivalent to removeAttr(name). (Was roughly this before ...
authorJohn Resig <jeresig@gmail.com>
Tue, 28 Sep 2010 15:12:13 +0000 (11:12 -0400)
committerJohn Resig <jeresig@gmail.com>
Tue, 28 Sep 2010 15:12:13 +0000 (11:12 -0400)
src/attributes.js
test/unit/attributes.js

index cb9f2cf5e073e997d6c04a86e9400cfa8b4eedcc..ec4841bd3f4f27d271f01a5388e8fd39ca97e2e0 100644 (file)
@@ -295,7 +295,14 @@ jQuery.extend({
                                                jQuery.error( "type property can't be changed" );
                                        }
 
-                                       elem[ name ] = value;
+                                       if ( value === null ) {
+                                               if ( elem.nodeType === 1 ) {
+                                                       elem.removeAttribute( name );
+                                               }
+
+                                       } else {
+                                               elem[ name ] = value;
+                                       }
                                }
 
                                // browsers index elements by id/name on forms, give priority to attributes.
index 3326dfe890c8c6e4cfab1bae5b7dbd6269cd5d5d..de7754b8a5482f2e01e96c658c8d520b89d8b914 100644 (file)
@@ -98,15 +98,18 @@ test("attr(Hash)", function() {
 });
 
 test("attr(String, Object)", function() {
-       expect(23);
+       expect(24);
+
        var div = jQuery("div").attr("foo", "bar"),
                fail = false;
+
        for ( var i = 0; i < div.size(); i++ ) {
                if ( div.get(i).getAttribute('foo') != "bar" ){
                        fail = i;
                        break;
                }
        }
+
        equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
 
        // Fails on IE since recent changes to .attr()
@@ -114,6 +117,8 @@ test("attr(String, Object)", function() {
 
        jQuery("#name").attr('name', 'something');
        equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
+       jQuery("#name").attr('name', null);
+       equals( jQuery("#name").attr('title'), '', 'Remove name attribute' );
        jQuery("#check2").attr('checked', true);
        equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
        jQuery("#check2").attr('checked', false);