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

Changing selected property of an element should not affect attribute #2648

Open
andolf opened this issue Jul 14, 2022 · 1 comment
Open

Changing selected property of an element should not affect attribute #2648

andolf opened this issue Jul 14, 2022 · 1 comment

Comments

@andolf
Copy link

andolf commented Jul 14, 2022

When changing the selected property using .prop() you're changing the inherent state of the element. The attribute selected only defines the initial state of the document when parsed.

However, when changing the property selected it now also alters the attribute.

Simple code example:

const {load} = require("cheerio");

const $ = load(`<select>
<option>1</option>
<option selected>2</option>
</select>`);

const option = $("option").eq(0);

console.log(option.prop("selected")); // should be false
console.log(option.attr("selected")); // should be undefined

option.prop("selected", true);

console.log(option.prop("selected")); // should be true
console.log(option.attr("selected")); // should still be undefined, but is "selected"

Notable is that this same behaviour also applies to the checked property.

jQuery's docs on .prop()

@fb55
Copy link
Member

fb55 commented Nov 4, 2022

Interesting issue. cheerio's prop also accesses attributes by default, which is non-standard. I quite like the way prop works in Cheerio at the moment, and will have to think about the best way forward.

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