How to find sibling HTML nodes using Cheerio and NodeJS
When you already have a reference element in a parsed HTML document, the Cheerio siblings method returns the other elements that share its parent.
This guide shows the full sequence: load HTML, select a target, call siblings(), then iterate the result and read each node's text. It also explains what an empty result usually means and how to choose a selector before traversal.
What the siblings method returns
A Cheerio object wraps a selection. Calling siblings() on that selection produces a new collection containing the sibling nodes of each selected element. The original element is not included in that collection. In the example below, the second paragraph is the reference point; the first and third paragraphs are its siblings. The method is part of the Cheerio API, so the same traversal pattern works in a Node.js script after the HTML has been loaded with cheerio.load().
Complete example: select, traverse, and print
The following example is the supplied working code module. It loads a small HTML fragment, selects the second paragraph with p:nth-of-type(2), retrieves all sibling elements, and prints each sibling's text. The output contains the first and third paragraphs, confirming that the selected paragraph itself is excluded.
Input and output map
Use this map to trace what each step produces:
- Input HTML string: three paragraph elements inside one div.
- cheerio.load(html): returns a Cheerio function, commonly assigned to $.
- $('p:nth-of-type(2)'): selects the second paragraph as the reference element.
- secondParagraph.siblings(): returns a Cheerio collection of the other paragraph elements.
- siblingElements.each((i, element) => ...): visits each sibling in the collection.
- $(element).text(): reads the text content of the current sibling.
- Output: the text of the first paragraph and the text of the third paragraph.
Selector checklist before calling siblings()
The siblings method depends on a correct reference selection. Before traversal, check the following:
- Does the selector match exactly the element you want as the reference point? The example uses p:nth-of-type(2) for the second paragraph.
- If the page has several groups of paragraphs, does the selector stay within the intended parent? A selector that matches elements in different containers will produce sibling collections from each container.
- Are you selecting by position, class, attribute, or tag? The supplied note says p:nth-of-type(2) can be replaced with any other appropriate selector.
- After calling siblings(), do you need to filter the collection further? The method returns all siblings, not only the ones with a particular class or attribute.
- Are you reading text, attributes, or both? The example reads text with $(element).text(); other extraction steps would use the same iteration pattern.
Why the sibling result may be empty
An empty sibling collection is usually a selection problem, not a missing method. Two common causes are: the selector matched no element, so there is no reference from which to traverse; or the matched element has no siblings under its parent. Check the selector first, then inspect the parent structure. If the selector matches more than one element, siblings() operates on each matched element, so the combined result may be larger than expected. The example avoids that ambiguity by selecting a single paragraph with nth-of-type.
Frequently asked questions
What does the Cheerio siblings method return?
It returns a Cheerio collection of the sibling nodes of each selected element. The selected element itself is not part of that collection. In the example, selecting the second paragraph and calling siblings() yields the first and third paragraphs.
How do I loop over sibling elements in Cheerio?
Call .each() on the sibling collection. The callback receives an index and the current element; wrap the element with the Cheerio function, as in $(element).text(), to read its content.
Can I use a different selector instead of p:nth-of-type(2)?
Yes. The supplied note says p:nth-of-type(2) is used to select the second paragraph and can be replaced with any other appropriate selector. The rest of the traversal pattern stays the same.
Why does siblings() return nothing?
The selector may not have matched any element, leaving no reference point for traversal. Alternatively, the matched element may have no siblings under its parent. Verify the selector and the surrounding HTML structure before changing the traversal code.