Skip to content

Selecting Values Between Two Nodes in Cheerio and Node.js

When you need everything that sits between two markers in a page — for example, the paragraphs under one heading and before the next — Cheerio can collect that in-between range without writing a manual loop over the whole document.

OUTPUT FORMATSHTMLJSONMarkdown

The approach below pairs forward traversal with a simple text extraction step.

Mark the boundaries, then traverse forward

Load the HTML with cheerio.load so the $ function can query it. Choose the earlier marker with a CSS selector and store it as the start, then choose the later marker as the end. Calling nextUntil on the start, with the end as its argument, returns every sibling element encountered after the start and before the end. That set is the range you actually want, rather than every element on the page.

Extract text and return a plain array

Once the in-between elements are collected, map over them and read the text of each one. Calling get at the end converts the result into a normal JavaScript array. In the worked example, the markers are the first-level and second-level headings, so the collected range holds the two paragraphs between them and the printed result is an array containing Paragraph 1 and Paragraph 2.

Checks before you rely on the result

Check the marker order before trusting the output: reversing them changes which direction you need to traverse. Confirm the markers are siblings on the same traversal path, because nextUntil follows sibling order. Remember that both boundary elements are excluded from the result. Finally, decide whether text alone is enough, or whether you need attributes from the same elements — if so, read those attributes inside the same map callback instead of only calling text.

Between-nodes selection at a glance
StepWhat it doesResult
Load HTML with cheerio.loadMakes the document queryable through $A Cheerio object ready for selectors
Select start and end nodesIdentifies the two boundary elementsTwo element references
Call nextUntil(endNode) on the startCollects siblings after the start and before the endOnly the elements between the markers
Map each element to its text, then call getReads text from every collected elementA plain array of strings

Based on the worked example in protected module code0001.

Frequently asked questions

Which node should be the start and which should be the stop?

Pick the element that comes first in document order as the start, and the later element as the stop. Traversal then collects siblings encountered after the start and before that stop, so the two markers need to be in the right order and share a traversal path.

Are the start and end nodes included in the result?

The result contains only what lies between them. The start element itself and the stop element itself are excluded, which is why the example returns the two paragraphs and not the two headings.

How do I turn the collected elements into plain values?

Use map to visit each collected element in turn, read its text, and then call get to turn the mapped result into a plain array that is easy to log, store, or pass onward.