jQuery Multiple Selector “selector1, selector2, selectorN”

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Comma OR union

What You’ll Learn

The multiple selector — written selector1, selector2, selectorN — combines several CSS selectors with the comma combinator. Each selector runs independently and jQuery returns the union of all matches. It is the efficient way to target disparate elements in one query — available since jQuery 1.0.

01

Syntax

sel1, sel2

02

Logic

OR union

03

Order

Document order

04

Official

div, span, p.myClass

05

vs space

OR not descendant

06

Since 1.0

CSS + jQuery

Introduction

Real pages mix many element types — headings, paragraphs, buttons, and special classes. Running separate queries for each group works, but the comma combinator lets you select them all at once with a single $() call.

The syntax selector1, selector2, selectorN is standard CSS. jQuery has supported it since version 1.0. Each comma-separated part is evaluated on its own; the returned jQuery object contains every element that matched any of the selectors — a union, not an intersection.

Understanding the Multiple Selector

Think of each selector as a separate search. The comma merges every result into one collection — like asking “find all divs or all spans or all paragraphs with class myClass.”

  • $("div, span") → every div and every span on the page.
  • $("h1, h2, .subtitle") → all h1s, all h2s, and every element with class subtitle.
  • $("div span")not a multiple selector — space means descendant, not OR.
  • $("div.active") → chained AND — divs that also have class active.
💡
Beginner Tip

Comma = OR (union). Space = descendant. Chained tokens = AND. Do not confuse $("div, span") with $("div span") — they select completely different sets of elements.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( "selector1, selector2, selectorN" )
// shorthand:
$( "selector1, selector2, selectorN" )

// common patterns:
$( "div, span, p.myClass" )
$( "h1, h2, .subtitle" )

Parameters

  • selector1, selector2, … — any valid CSS selectors separated by commas. Each is evaluated independently.
  • Whitespace around commas is optional: div,span and div, span behave the same.

Return value

  • A jQuery object containing the combined results of all specified selectors (union).
  • Elements appear in document order, not in the order selectors were listed.
  • Duplicates are removed — each DOM node appears at most once.

Official jQuery API examples

jQuery
// Example 1 — style disparate elements at once
$( "div, span, p.myClass" ).css( "border", "3px solid red" );

// Example 2 — result order follows the DOM, not the selector list
$( "div, p, span" ).map( function() {
  return this.tagName;
} ).get().join( ", " );

⚡ Quick Reference

Selector patternLogicWhat it matches
div, spanOR (comma)All divs and all spans
div spanDescendant (space)Spans nested inside divs
div.activeAND (chained)Divs with class active
input[type="text"][required]AND (multi-attr)Text inputs that are required
h1, h2, .subtitleOR (comma)All h1s, h2s, and .subtitle elements
$("div").add("span")OR (method)Same union via .add()

📋 Comma OR vs .add() vs multi-attribute AND vs descendant space

Four different ways to combine selectors — only comma and .add() produce a union.

Comma OR
$("div, span")

All divs and all spans — one query

.add() method
$("div").add("span")

Same union, built from existing collection

Multi-attr AND
[type="text"][required]

Both attributes must match

Descendant space
$("div span")

Spans inside divs only

Examples Gallery

Example 1 follows the official jQuery API demo. Examples 2–5 cover document order, .add() migration, comma vs descendant space, and styling headings together. Use the Try-it links to run each snippet.

📚 Official jQuery Demo

Apply a red border to all divs, all spans, and p elements with class myClass in one call.

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

Official jQuery demo — every div, every span, and every p.myClass gets a 3px solid red border.

jQuery
$( "div, span, p.myClass" ).css( "border", "3px solid red" );

// Matches: every <div>, every <span>, every <p class="myClass">
// Skips: p without myClass, other tags without matching selectors
Try It Yourself

How It Works

jQuery evaluates each comma-separated selector independently, merges the results, and applies .css() to the union. One query replaces three separate $() calls.

Example 2 — Document Order, Not Selector List Order

Official jQuery demo — map tag names to show that results follow DOM order, not the order selectors were written.

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

console.log( order );
// If a <span> appears before a <div> in the HTML,
// SPAN comes first — even though "div" is listed first
Try It Yourself

How It Works

The comma lists selectors for evaluation order only. The returned jQuery object always sorts matched nodes by their position in the document tree — a detail that matters when iterating or chaining methods.

📈 Practical Patterns

.add() migration, comma vs space, and heading groups.

Example 3 — .add() Equivalent Migration

Build the same union programmatically when you already have a jQuery collection.

jQuery
// Comma — one static query
var $union = $( "div, span, p.myClass" );

// .add() — same result from an existing collection
var $same = $( "div" ).add( "span" ).add( "p.myClass" );

console.log( $union.length === $same.length ); // true
Try It Yourself

How It Works

Use comma for a single declarative query. Reach for .add() when selectors are built dynamically or when extending an existing collection at runtime.

Example 4 — Comma OR vs Descendant Space

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

jQuery
var commaCount = $( "div, span" ).length;
var spaceCount = $( "div span" ).length;

console.log( "comma (OR):", commaCount );
console.log( "space (descendant):", spaceCount );

// comma → all divs + all spans
// space → only spans nested inside a div
Try It Yourself

How It Works

A comma between selectors means “or.” A space means “inside.” Mixing them up is one of the most common selector mistakes — always read comma as union and space as hierarchy.

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

Apply shared typography to all top-level headings and subtitle-class elements in one query.

jQuery
$( "h1, h2, .subtitle" ).css({
  "font-weight": "600",
  "margin-bottom": "0.5em"
});

// Matches: every h1, every h2, every element with class subtitle
// Efficient way to style disparate heading-like elements at once
Try It Yourself

How It Works

Mixing element types and classes in one comma list is a practical pattern for shared styling rules — cleaner than three separate queries or repeated CSS blocks.

🚀 Common Use Cases

  • Shared styling$("h1, h2, h3") or $("h1, h2, .subtitle") for typography.
  • Form controls$("input, select, textarea") to validate or disable all inputs at once.
  • Hide/show groups$(".tab-panel, .tab-content") to toggle related sections together.
  • Event binding$("a, button, .clickable") for unified click handlers.
  • Reset states$(".error, .warning, .success") to clear message classes.
  • Accessibility$("[role='button'], button, a") to focus or announce interactive elements.

🧠 How jQuery Evaluates Multiple Selectors

1

Split on commas

Parse the selector string into individual CSS selectors.

Parse
2

Evaluate each

Run every selector independently against the document (or context).

OR
3

Merge union

Combine all matches; remove duplicates so each node appears once.

Union
4

Sort by document order

Return a jQuery object ordered by DOM position, not selector list order.

📝 Notes

  • Available since jQuery 1.0 — standard CSS comma combinator.
  • Comma means OR (union), not AND.
  • Result order follows the document tree, not the order selectors were listed.
  • Alternative: .add() method produces the same union from an existing collection.
  • Not the same as descendant space (div span) or chained AND (div.active).
  • Not the same as multiple-attribute selectors ([a][b]) which require both attributes.

Browser Support

The multiple selector is part of CSS and works in jQuery 1.0+. Evergreen browsers support comma-separated selectors in native querySelectorAll. IE8+ supports multi-selector queries with jQuery fallbacks where needed.

CSS · jQuery 1.0+

jQuery "sel1, sel2, selN"

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

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: Ideal for selecting disparate elements in one query. Remember document order, not selector list order, governs the returned collection.

Conclusion

The multiple selector selector1, selector2, selectorN combines several CSS selectors with comma OR logic. jQuery’s official demo, $("div, span, p.myClass").css("border", "3px solid red"), styles every matching element in a single call.

Use comma for unions; use space for descendants; use chained tokens or multiple-attribute selectors for AND. When you already hold a jQuery collection, .add() builds the same union programmatically. Results always arrive in document order.

💡 Best Practices

✅ Do

  • Use comma to select disparate elements in one query
  • Remember results follow document order, not selector list order
  • Prefer comma over multiple separate $() calls for static unions
  • Use .add() when building unions dynamically at runtime
  • Group related styling: $("h1, h2, .subtitle")

❌ Don’t

  • Confuse $("div, span") with $("div span")
  • Assume elements appear in the order you listed selectors
  • Use comma when you need AND — chain selectors or use [a][b]
  • Repeat the same comma list in many places — extract a variable or CSS class
  • Over-combine unrelated selectors when scoping would be clearer

Key Takeaways

Knowledge Unlocked

Five things to remember about the multiple selector

Comma OR union in one query.

5
Core concepts
DOM 02

Order

Document order

Rule
.add 03

Alternative

Same union

Method
04

Not space

OR vs descendant

Compare
div 05

Official

Red border demo

Demo

❓ Frequently Asked Questions

The comma combinator combines several selectors into one query. $("div, span, p.myClass") returns every element that matches any of the listed selectors — all divs, all spans, and all p elements with class myClass — in a single jQuery collection.
Comma is OR (union). Each selector is evaluated independently and results are merged. Chained selectors without a comma — like div.active or input[type="text"][required] — use AND logic instead.
The jQuery object lists elements in document order (DOM tree order), not in the order you wrote the selectors. $("div, p, span") may return a span first if it appears before a div in the HTML, even though div is listed first.
$("div, span") selects all divs and all spans anywhere on the page (OR). $("div span") selects spans that are descendants of a div (nested relationship). Comma widens the match; space narrows it.
Use .add() when you already have a jQuery collection and want to append more elements programmatically — for example $("div").add("span"). Both comma and .add() produce a union; comma is cleaner for a single static query string.
Yes. The comma combinator is standard CSS and has worked in jQuery since 1.0. Modern browsers implement it natively through querySelectorAll.
Did you know?

When you write $("div, p, span"), the returned jQuery object lists elements in document order — the order they appear in the HTML tree — not in the order you listed the selectors. A span that appears before a div in the markup comes first, even though div is written first in the selector string.

Continue to the :has() Selector

After comma unions, learn how to select elements that contain matched descendants with :has().

:has() selector 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.

6 people found this page helpful