$.text() return empty string
See original GitHub issueFrom my understanding of the docs, once you cheerio.load( someMarkup )
then calling .html or .text on the returned variable should return the entire input markup and the input markup less HTML tags, respectively. However, $.html()
(as used in the readme) works, but $.text()
only returns an empty string.
Put another way, I would expect these two snippets to be equivalent, but where I would expect “Some Text String” for the result of $1.text()
, an empty string is returned instead: .text()
only works if a selector is provided first.
var cheerio = require('cheerio');
// comments represent actual output
console.log('Unwrapped:')
var $1 = cheerio.load('Some <em>Text</em> String');
console.log($1.html()); // "Some <em>Text</em> String"
console.log($1.text()); // ""
console.log('Wrapped:');
var $2 = cheerio.load('<h1>Some <em>Text</em> String</h1>');
console.log($2('h1').html()); // "Some <em>Text</em> String"
console.log($2('h1').text()); // "Some Text String"
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
$('#inputElement').text() returns empty string and a solution for it
I have a problem and a solution. Then I use $('#inputElement').text() to get the value of an input element, I receive an empty...
Read more >jQuery .text() returning empty string and .html ... - Stack Overflow
When you write $("#msg_68 a") , you are trying to access this element from the DOM in your current page and not from...
Read more >DetailsView and TextBox.Text returning empty string - MSDN
User1663022000 posted. Hi, I've got a TextBox control inside a templatefield in my detailsview. I'm able to retrieve the actual textbox object with...
Read more >jquery .text() function always returns an empty string - Reddit
jquery .text() function always returns an empty string ... a ton of jQuery experience, but I am trying to get the .text() of...
Read more >Empty string - Exceljet
When an empty string is the result of a formula, it looks like the cell is blank. So, empty strings are commonly used...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey Kadam, thanks for the report!
The trouble here is in the distinction between the static
$.text
method and the$.prototype.text
instance method.$.prototype.text
has the behavior you want, but it requires you make an instance first–a bit tricky when your document does not have a top-level element.$.text
would seem like the right alternative (given the documented behavior of$.html
as you point out), but it doesn’t actually do this… for some reason.I’ve submitted gh-855 to enable your use case for
$.text
and to promote API parity. As I mention there, the new feature is technically backwards breaking, although only of undocumented/untested functionality. I’m going to defer to my fellow maintainers to decide on if/how/when we’d like to merge that patch.In the mean time, I have two alternatives that will work for you today, both of which use documented, tested APIs:
$.text($.root())
$.root().text()
Do either of those work for you?
Fixed in #855 (not released yet)