Get the text of the current node only

Kevin picture Kevin · Feb 14, 2017 · Viewed 10.5k times · Source

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?

Answer

Rentrop picture Rentrop · Feb 16, 2017

You can do this:

console.log($('div').contents().first().text()) # hello