cheeriojs / cheerio

The fast, flexible, and elegant library for parsing and manipulating HTML and XML.
https://cheerio.js.org
MIT License
28.73k stars 1.64k forks source link

Can't get button value using the val function #3951

Open ribeirobreno opened 4 months ago

ribeirobreno commented 4 months ago

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
ribeirobreno commented 4 months ago

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:

fb55 commented 3 months ago

Thanks for the links! Happy to accept PRs for this.

PRASUN-SITAULA commented 2 months ago

Hey @fb55 can I work on the issue?

fb55 commented 2 months ago

For sure! PRs welcome

PRASUN-SITAULA commented 2 months ago

Thanks @fb55 Could you please let me know which part of the source code I should change for? Thanks!