Skip to content

Commit

Permalink
Make .attr(name, null) equivalent to removeAttr(name). (Was roughly t…
Browse files Browse the repository at this point in the history
…his before - but is now consistent across platforms). Fixes #6341.
  • Loading branch information
jeresig committed Sep 28, 2010
1 parent cf672a2 commit c7c0677
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/attributes.js
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion test/unit/attributes.js
Expand Up @@ -98,22 +98,27 @@ 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()
// ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );

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);
Expand Down

0 comments on commit c7c0677

Please sign in to comment.