Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function .attr() - handle value undefined like jQuery #1757

Merged
merged 1 commit into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function setAttr(el, name, value) {
*/
exports.attr = function (name, value) {
// Set the value (with attr map support)
if (typeof name === 'object' || arguments.length > 1) {
if (typeof name === 'object' || value !== undefined) {
if (typeof value === 'function') {
return domEach(this, function (i, el) {
setAttr(el, name, value.call(el, i, el.attribs[name]));
Expand All @@ -127,7 +127,7 @@ exports.attr = function (name, value) {
});
}

return getAttr(this[0], name);
return arguments.length > 1 ? this : getAttr(this[0], name);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion test/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('$(...)', function () {
it('(chaining) setting attr to undefined returns a $', function () {
var $pear = $('.pear').attr('foo', undefined);
expect($('.pear')).toHaveLength(1);
expect($('.pear').attr('foo')).toBe('undefined'); // TODO this is stringified undefined
expect($('.pear').attr('foo')).toBeUndefined();
expect($pear).toBeInstanceOf($);
});
});
Expand Down