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 nodes? Requesting .contents()

See original GitHub issue

I’m trying to get text nodes with cheerio and I cannot find a way to do this. The only way that I am aware of is calling .contents() in jQuery to retrieve all elements (including text nodes and comment nodes). Example:

var markup = '<div>Something <b>bold</b></div>';
var $ = cheerio.load(markup);
console.log($('div').contents().first().text()); // "Something "

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Deathspikecommented, Jan 12, 2013

That is indeed pretty easy, thanks for the hints. I didn’t actually jump into the source code yet, but I’ve just edited my second response with the proper implementation (I guess). It would be nice to have this appear in the official branch at some point. Thanks for the feedback!

var contents = exports.contents = function() {
  if (!this[0] || !this[0].children) return this;
  return this.make(this[0].children);
};
0reactions
Deathspikecommented, Jan 12, 2013

Ah, indeed. I missed that. I guess it will become something like this

var contents = exports.contents = function() {
  var all = [];
  _.each(this, function(el, i) {
    _.each(el.children, function(ch, j) {
      all.push(ch);
    });
  })
  return this.make(all);
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.textContent - Web APIs | MDN
The textContent property of the Node interface represents the text content of the node and its descendants.
Read more >
How to get the text node of an element? - Stack Overflow
I will update the answer to show it. You need text() because the the filter function returns the nodes themselves, not the contents...
Read more >
Chapter 16: HTML Text Nodes in JavaScript - Medium
This is correct because our querySelector() selects the second p element. ... To see what this text content is, we can ask for...
Read more >
Anatomy of an HTTP Transaction | Node.js
When an HTTP request hits the server, node calls the request handler function with a few handy objects for dealing with the transaction,...
Read more >
Replacing Text Nodes With jQuery - Ben Nadel
In rare cases, however, we do want to get at the Text nodes contained within an element. To do this, we can use...
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