Fred J. Fred J. - 6 months ago 17
jQuery Question

Getting text from first sibling of given element

This jQuery like selector "cheerio" tries to get the text "milk" from the html. but fails. '$' is the cheerio object.

How can it be done? Thanks

<!-- food -->
<td class="label" valign="top" align="right" style="white-space:nowrap">
<td class="displayValue" valign="top">

$('*').contents().filter(function () {
if (this.nodeType == 8) { //found the comments node / title

// this produced both the texts 'drinking' and 'milk'



This code may be helpful for you.:

$('*').contents().filter(function () {
  if (this.nodeType == 8) {           //found the comments node / title
    console.log($(this.nextSibling).text());  //did work

For some reason, not obvious to me, $(this).next(); don't work properly. So, I used the native JS nextSibling;