Skip to content

Fix: Make .attr() remove attributes when value is undefined#5072

Open
veeceey wants to merge 1 commit intocheeriojs:mainfrom
veeceey:fix/issue-3307-undefined-attr
Open

Fix: Make .attr() remove attributes when value is undefined#5072
veeceey wants to merge 1 commit intocheeriojs:mainfrom
veeceey:fix/issue-3307-undefined-attr

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 13, 2026

Summary

Fixes #3307 - .attr('foo', undefined) now removes the attribute, matching the behavior of .attr('foo', null).

Previously, setting an attribute to undefined would not remove it, while setting it to null would. This was inconsistent with jQuery's behavior and user expectations.

Changes

  • Updated setAttr() function to handle both null and undefined values
  • Fixed attr() implementation to use arguments.length check instead of value !== undefined to properly detect when undefined is explicitly passed
  • Updated TypeScript type signatures to include undefined in the value parameter
  • Added comprehensive tests for undefined attribute removal (both single attribute and map-based updates)

Test Results

All 129 tests pass, including 3 new tests that verify:

  1. Single attribute removal with undefined value
  2. Map-based attribute updates with undefined values
  3. Chaining behavior when setting attributes to undefined

Manual Testing

Verified the fix with the exact code from the issue report - both null and undefined now correctly remove attributes.

Previously, .attr('foo', undefined) would not remove the attribute,
while .attr('foo', null) would. This inconsistency has been fixed
to match jQuery's behavior where both null and undefined remove
the attribute.

Changes:
- Updated setAttr() to handle both null and undefined values
- Changed attr() implementation to use arguments.length check
  instead of value !== undefined to properly detect when
  undefined is explicitly passed as an argument
- Updated TypeScript type signatures to include undefined
- Added comprehensive tests for undefined attribute removal

Fixes cheeriojs#3307
@veeceey
Copy link
Author

veeceey commented Feb 19, 2026

Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Can't set existing attribute as undefined

1 participant