Can't get button value using the val function
In the latest version (0.22.0), getting a button value is not possible with the .val() function.
import * as cheerio from "cheerio";
let ch = cheerio.load(`<form><button value="something">I'm a button</button></form>`);
console.log(ch('button').val());
console.log(ch('button').attr('value'));
The code snippet above will return:
undefined
something
To match jQuery behavior, it should return:
something
something
Adding more context, jQuery documentation is a little misleading in stating that "val" will try to get the value from a specific list of tags. In fact, it will get the "value" attribute from anything that has this attribute in their DOM prototype. See:
- https://github.com/jquery/jquery/blob/3.7.1/src/attributes/val.js (only checks for the attribute existence, handles set vs get and handles special cases)
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements#value
(aside from object and fieldset, all tags listed here should return it's value from a
.valfunction call) - https://developer.mozilla.org/en-US/docs/Web/HTML/Element#forms (more form elements)
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement#htmlbuttonelement.value
https://developer.mozilla.org/en-US/docs/Web/API/HTMLDataElement/value
https://developer.mozilla.org/en-US/docs/Web/API/HTMLOutputElement
(those have a value attribute, so they will get read by
.val)
Thanks for the links! Happy to accept PRs for this.
Hey @fb55 can I work on the issue?
For sure! PRs welcome
Thanks @fb55 Could you please let me know which part of the source code I should change for? Thanks!