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

$.attr('value') returns incorrect result for an <option> without a value attribute #3237

Open
36degrees opened this issue Jun 9, 2023 · 0 comments

Comments

@36degrees
Copy link

36degrees commented Jun 9, 2023

Steps to reproduce

const cheerio = require('cheerio')

const $ = cheerio.load(`
<select class="govuk-select" id="colors" name="colors">
  <option>Red</option>
  <option>Green</option>
  <option>Blue</option>
</select>
`)

console.log($('option:first-child').attr('value'))

Actual result

'Red' is logged to the console, which is the inner text of the option

Expected result

undefined should be logged to the console, because the element does not have a value attribute.

This would match how attr works both according to jQuery's documentation and when compared to it's actual behaviour (CodePen).

Further details

It looks like this may be caused by this code in the getAttrfunction:

// Mimic the DOM and return text content as value for `option's`
if (elem.name === 'option' && name === 'value') {
return text(elem.children);
}

This was introduced as part of #671 but should probably have only affected the val function as described in the issue it was trying to fix.

I'd be open to raising a pull request with a fix if that would be helpful.

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

1 participant