Fred J. Fred J. - 1 year ago 105
Javascript Question

extract DOM element with cherrio

This Meteor server code fails to extract the text "789" using the cherrio object $ as the docs suggests.

$('td[headers=x]').each(function() {
console.log($(this).next().html().trim()); //=> 456
console.log($(this).next('td[headers="y"]').html().trim()); //<-- fail

<td headers="x" class="bodyTextSmall">
<td headers="xx" class="bodyTextSmall">
<td headers="y" class="bodyTextSmall">

Any idea why? Thanks

Answer Source

.next() will only return the immediate next sibling - if a selector is used then it must match. Your headers="yy" is not the immediate next sibling but the second next. Use .siblings() with the selector instead:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download