jQuery Multiple Selector “selector1, selector2, selectorN”

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is the Multiple Selector?

The multiple selector combines several CSS selectors with commas. jQuery returns the union of all matches — every element that matches any of the listed selectors — in one collection. Available since jQuery 1.0 (standard CSS).

Remember
$("selector1, selector2, selectorN")

$("div, span")           → all divs OR all spans
$("div, span, p.myClass") → divs, spans, and p.myClass

$("div span")   → descendant (space), not OR
$("div.active") → AND (chained), not OR
UsePrefer
Union in one query$("div, span")
Extend a collection$("div").add("span")
Nested elements$("div span")

How to Use the Multiple Selector

List selectors separated by commas, then chain any jQuery method on the combined set.

  1. Decide which unrelated groups you need (tags, classes, IDs, attributes).
  2. Join them with commas: $("h1, h2, .subtitle").
  3. Apply one action to the whole set — style, events, show/hide, and so on.
  4. Remember: results are in document order, not selector-list order.
PieceRequired?Role
selector1 … selectorNYes (at least one)Any valid CSS/jQuery selectors
CommaYes (for a union)OR combinator between selectors
Spaces around commasOptionaldiv,span equals div, span

Pseudocode

Pseudocode
parts = split(selectorString, by comma)
matches = empty set

for each part in parts:
    matches = matches ∪ find(part)

remove duplicates from matches
sort matches by document order
return jQuery(matches)

Cheat Sheet

Keep these facts handy while writing comma selectors.

TopicDetail
Syntax$("sel1, sel2, selN")
LogicOR (union), not AND
OrderDocument order (DOM tree)
DuplicatesEach element appears once
vs spacediv, span ≠ div span
vs .add()Same union; .add() for dynamic sets
SincejQuery 1.0 (standard CSS)

Live Preview

Pick a selector against a small sample DOM and see which tags match (and in what order).

Sample markup mirrors the official jQuery multiple-selector demos.

Result
$("div, span, p.myClass")
matched: 5
order: DIV, P, SPAN, DIV, SPAN

/* comma = OR (union) */

jQuery Examples

Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Official Demo: div, span, p.myClass

Select every div, every span, and every p.myClass in one query (jQuery API example).

jQuery
var tags = $("div, span, p.myClass").map(function () {
  return this.tagName;
}).get().join(", ");

console.log(tags);
// DIV, P, SPAN  (plus any other matching nodes)
Try It Yourself

How It Works

  1. Split on commas. jQuery treats div, span, and p.myClass as three separate searches.
  2. Union the hits. Every matching node is kept; non-matching paragraphs are ignored.
  3. Map tag names. .map() reads each element’s tagName for a clear console list.
  4. One query. You avoid three separate $() calls for the same styling or logic.

Example 2 — Document Order, Not Selector List Order

Show that the returned set follows DOM order, even if you list div first.

jQuery
// Markup order: span, p, div, span, div
var order = $("div, p, span").map(function () {
  return this.tagName;
}).get().join(", ");

console.log(order);
// SPAN, P, DIV, SPAN, DIV
Try It Yourself

How It Works

  1. List selectors any way. Writing div first only chooses what to search, not result order.
  2. Scan the tree. Matches are sorted by where they sit in the HTML.
  3. Join for display. .get().join() prints the tag sequence clearly.
  4. Why it matters. Loops and chained methods run in document order, not selector-list order.

Example 3 — Comma vs .add()

Build the same union with a comma string or by extending a collection.

jQuery
var $comma = $("div, span");
var $added = $("div").add("span");

console.log($comma.length === $added.length); // true
console.log($comma.length);
Try It Yourself

How It Works

  1. Comma path. One string selects divs and spans together.
  2. .add() path. Start with divs, then append spans to that set.
  3. Compare lengths. Both collections hold the same union of nodes.
  4. When to choose. Static query → comma. Existing set or dynamic pieces → .add().

Example 4 — Comma OR vs Descendant Space

See how $("div, span") differs from $("div span") on the same markup.

jQuery
console.log("comma:", $("div, span").length);
console.log("space:", $("div span").length);

// comma → every div and every span
// space → only spans nested inside a div
Try It Yourself

How It Works

  1. Comma means OR. Outer divs and any span (inside or outside) all match.
  2. Space means inside. Only spans that are descendants of a div match.
  3. Count the difference. Lengths show how much wider the union is.
  4. Common mistake. Typing a space instead of a comma silently changes the meaning.

Example 5 — Style Headings Together: h1, h2, .subtitle

Group disparate heading-like elements and mark them in one call.

jQuery
$("h1, h2, .subtitle").each(function () {
  this.textContent = "[*] " + this.textContent;
});

console.log($("h1, h2, .subtitle").length);
Try It Yourself

How It Works

  1. Mix types. Element selectors and a class selector share one comma list.
  2. Iterate once. .each() runs on the whole union.
  3. Leave others alone. Plain paragraphs without .subtitle are not selected.
  4. Practical pattern. Shared typography or reset logic often needs this union.

Edge Cases & Pitfalls

Check these before relying on comma selectors in real projects.

trap

Comma ≠ space

$("div, span") is OR. $("div span") is descendant. One character changes the whole match set.

order

Document order, not list order

Do not assume results follow the order of selectors in the string. Iterate expecting DOM order.

and

Comma is not AND

Need both conditions on one element? Chain tokens (div.active) or use a multiple attribute selector.

scope

Over-broad unions

Huge comma lists can match more than you intend. Scope with a parent context: $("#panel").find("h1, h2").

Browser Support

The multiple selector is standard CSS and has worked in jQuery since 1.0. Modern browsers implement comma-separated selectors in native querySelectorAll.

✓ CSS · jQuery 1.0+

jQuery "sel1, sel2, selN"

Supported in all modern browsers. Native: document.querySelectorAll("div, span, p.myClass"). Universal CSS comma combinator.

100% Standard CSS selector
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Comma combinator Universal

Bottom line: Safe everywhere jQuery runs. Prefer comma for static unions; use .add() when extending an existing collection at runtime.

Performance Notes

ApproachCostBest for
$("a, b, c")One engine pass (union)Static disparate groups
$("a"); $("b"); $("c")Three separate queriesAvoid when you need one set
$("a").add("b")Extra merge stepDynamic / incremental unions

Official docs call the multiple expression combinator an efficient way to select disparate elements. Keep lists focused; scope under a container when the page is large.

Key Takeaways

  • Role: comma = OR union of several selectors in one $() call.
  • Order: results follow the DOM, not your selector list.
  • Not space: div, span ≠ div span.
  • Alternative: pair with .add() for dynamic unions.

One line: use $("sel1, sel2, selN") to select disparate elements efficiently in one query.

Frequently Asked Questions

It combines several selectors with commas into one query. $("div, span, p.myClass") returns every element that matches any listed selector — all divs, all spans, and all p.myClass elements — in a single jQuery collection.
Comma is OR (union). Each selector runs on its own and results are merged. Chained tokens without a comma — like div.active — use AND logic instead.
In document order (DOM tree order), not the order you wrote the selectors. $("div, p, span") may return a span first if it appears before a div in the HTML.
$("div, span") selects all divs and all spans (OR). $("div span") selects spans nested inside a div (descendant). Comma widens; space narrows.
Use .add() when you already have a jQuery collection and want to append more matches — for example $("div").add("span"). For one static query string, prefer the comma combinator.
Since jQuery 1.0. It is standard CSS and works via native querySelectorAll in modern browsers.

Next: :has() Selector

Select elements that contain matched descendants with the :has() selector.

:has() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful