Skip to content

How to Find HTML Elements by Multiple Tags with Cheerio

When a page mixes related content across different tags, you do not need a separate query for each one.

OUTPUT FORMATSHTMLJSONMarkdown

Cheerio accepts a CSS selector list, so a single call can match every tag you name. This guide shows the selector syntax, a complete Node.js example, and a short checklist for deciding when a combined query is the right choice.

The selector-list approach

Separate tag selectors with a comma inside the same selector string. For example, a query for div and span elements uses the selector div, span. Cheerio returns one combined collection containing matches for either tag, in document order. You can then iterate that collection with .each() and extract what you need from each node, such as its text.

Complete Node.js example

The example below loads a small HTML string, selects div and span elements together, and prints the text of each match. The protected code module code0001 accompanies this page with the exact runnable contents. The expected output lists the three text values in the order they appear in the markup.

Inputs, selector, and output

The workflow has three clear stages. First, an HTML string is passed to cheerio.load(). Second, a selector string containing comma-separated tag names is evaluated. Third, the resulting collection is traversed, and each element's text is read. If you need attributes rather than text, the same collection can be queried for those values instead. The selector list controls which tags are included; the iteration controls what you do with each match.

When to combine tags in one query

A combined selector is useful when several tag types represent the same kind of content and you want to process them uniformly. It keeps the code shorter and avoids merging separate result sets yourself. Use separate queries instead when each tag needs different handling, different output fields, or a different order of operations. Also check that the comma is inside the selector string; placing it outside changes the meaning of the call.

Quick checklist

Before running the query, confirm the tag names are correct, the comma separates complete selectors, and the iteration handles every match. After running it, compare the number of printed values with the number of matching elements in the HTML. If the count differs, inspect whether nested matches or whitespace are affecting the text output.

Related Cheerio questions

The following related topics cover other common selection and extraction tasks in Cheerio.

Frequently asked questions

Can I select more than two tags in one Cheerio query?

Yes. Add each tag name to the selector list, separated by commas. The result is a single collection of all matching elements.

Does a comma-separated selector return elements in a particular order?

The matched elements appear in document order, not grouped by selector. Iterating the collection therefore follows the order of the HTML.

How is selecting multiple tags different from selecting multiple attributes?

A tag selector list matches element names, such as div and span. Attribute selectors match elements based on their attributes. The two approaches answer different questions and can be combined when needed.