question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$.text() return empty string

See original GitHub issue

From 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:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jugglinmikecommented, Apr 27, 2016

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:

  1. Passing the root node to the static method: $.text($.root())
  2. Calling the instance method on a selection for the root node: $.root().text()

Do either of those work for you?

0reactions
fb55commented, Feb 22, 2018

Fixed in #855 (not released yet)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found