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

Can't set existing attribute as undefined #3307

Open
walshyb opened this issue Jul 28, 2023 · 1 comment
Open

Can't set existing attribute as undefined #3307

walshyb opened this issue Jul 28, 2023 · 1 comment

Comments

@walshyb
Copy link

walshyb commented Jul 28, 2023

When setting an existing attribute to undefined on a Cheerio element, the the attribute does not get removed or set to undefined.

However, when setting the same attribute to null, the attribute gets removed from the element.

const $cheerio = cheerio.load('<ul><li class="pear" foo="bar">Pear</li></ul>');
const $pear = $cheerio('.pear');

// Setting attribute 'foo' to undefined
$pear.attr('foo', undefined);
const attr1 = $pear.attr('foo');

attr1 === 'bar'           // true
attr1 === undefined       // false
$pear.attr('foo')         // [Object: null prototype] { foo: 'bar' }

// Setting attribute 'foo' to null
$pear.attr('foo', null);
const attr2 = $pear.attr('foo');

attr2 === 'bar'           // false
attr2 === undefined       // true
att2 == null              // true
$pear.attr('foo')         // [Object: null prototype] { }
@fb55
Copy link
Member

fb55 commented Jul 29, 2023

undefined is treated as if no value is passed, and will result in a get call. I agree that that is surprising. Let's see what jQuery does and align with jQuery's behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants