16

In Cheerio, how do you get just the text of the current node only?

var cheerio = require('cheerio')

const htmlString = '<div>hello<span>world</span></div>'

$ = cheerio.load(htmlString, { ignoreWhitespace: true })

console.log($('div').text())  //helloworld
console.log($('span').text())  //world

How do you get just hello?

Kevin
  • 3,441
  • 6
  • 34
  • 40

1 Answers1

27

You can do this:

console.log($('div').contents().first().text()) # hello
Rentrop
  • 20,979
  • 10
  • 72
  • 100