jQuery Multiple Selector “selector1, selector2, selectorN”

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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
CallWhat it does
selector1 … selectorNAny valid CSS/jQuery selectors
CommaOR combinator between selectors
Spaces around commasdiv,span equals div, span

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Decide which unrelated groups you need (tags, classes, IDs, attributes).
Step 2Join them with commas: $("h1, h2, .subtitle").
Step 3Apply one action to the whole set — style, events, show/hide, and so on.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: :animated Selector — Match Elements Mid-Animation

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

:animated Selector — Match Elements Mid-Animation tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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