Skip to content

Commit c7c0677

Browse files
committed
Make .attr(name, null) equivalent to removeAttr(name). (Was roughly this before - but is now consistent across platforms). Fixes #6341.
1 parent cf672a2 commit c7c0677

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/attributes.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,14 @@ jQuery.extend({
295295
jQuery.error( "type property can't be changed" );
296296
}
297297

298-
elem[ name ] = value;
298+
if ( value === null ) {
299+
if ( elem.nodeType === 1 ) {
300+
elem.removeAttribute( name );
301+
}
302+
303+
} else {
304+
elem[ name ] = value;
305+
}
299306
}
300307

301308
// browsers index elements by id/name on forms, give priority to attributes.

test/unit/attributes.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,27 @@ test("attr(Hash)", function() {
9898
});
9999

100100
test("attr(String, Object)", function() {
101-
expect(23);
101+
expect(24);
102+
102103
var div = jQuery("div").attr("foo", "bar"),
103104
fail = false;
105+
104106
for ( var i = 0; i < div.size(); i++ ) {
105107
if ( div.get(i).getAttribute('foo') != "bar" ){
106108
fail = i;
107109
break;
108110
}
109111
}
112+
110113
equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
111114

112115
// Fails on IE since recent changes to .attr()
113116
// ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
114117

115118
jQuery("#name").attr('name', 'something');
116119
equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
120+
jQuery("#name").attr('name', null);
121+
equals( jQuery("#name").attr('title'), '', 'Remove name attribute' );
117122
jQuery("#check2").attr('checked', true);
118123
equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
119124
jQuery("#check2").attr('checked', false);

0 commit comments

Comments
 (0)