Skip to content

Can't set existing attribute as undefined #3307

@walshyb

Description

@walshyb

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] { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions