jQuery .add() Method

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

What Is .add()?

.add() (since jQuery 1.0) builds a new jQuery object from the union of the current matched elements and extras you pass in — selector, DOM node(s), HTML string, or another jQuery object (1.1+). It does not insert nodes into the page.

Remember
$("p").add("div")                 // union of p + div
$("li").add("p")                  // list items + paragraph
$("p").add(document.getElementById("a"))
$("p").add("<b>hi</b>")           // HTML in memory only

// Wrong — original unchanged:
//   pdiv.add("div");
// Right:
//   pdiv = pdiv.add("div");
NeedUse
Merge by selector$("p").add("span")
Add a DOM node$("p").add(element)
Exclude instead.not()
Undo in a chain.end()
Insert into the page.append() (not .add())

How to Use .add()

Start with a set, merge extras, then style or act on the combined collection.

  1. Select a starting set (e.g. $("div") or $("li")).
  2. Call .add(selector), .add(element), .add(html), or .add($other).
  3. Chain a method on the new set, or assign: set = set.add(…).
  4. Remember: since 1.4, same-document results are in document order.
  5. To put HTML from .add() on the page, chain an insertion method.
PieceRequired?Role
Matched setYesStarting collection
Selector / element / htmlOne formExtras to merge (1.0+)
jQuery selectionOne formMerge another set (1.1+)
selector, contextOptionalScoped search (1.4+)
Return value—New jQuery union (must capture or chain)

Pseudocode

Pseudocode
call $(set).add(extra)

result = copy of set
extraElements = resolve(extra)   // selector / node / html / jQuery
for each e in extraElements:
    if e not already in result:
        result.add(e)

if all elements share one document:
    sort result in document order   // since jQuery 1.4

return result as a NEW jQuery object
// original set is unchanged

Cheat Sheet

Keep union vs insert vs exclude clear.

TopicDetail
Syntax.add(sel|el|html|jQuery[, ctx])
Since1.0 · jQuery object 1.1 · context 1.4
OrderDocument order (same document, 1.4+)
Mutates original?No — reassign or chain
vs .append()Collection vs DOM insert
vs .not()Union vs exclude
Reverse.not() or .end()

Live Preview

Merge different node types, then list the combined set in document order.

Box A
Box B

A paragraph

A span

Official idea: .add() unions sets; same-document results sort in document order.

Result
Ready — choose a call and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

Example 1 — Official: Border Divs, Then Add Paragraphs

Only early chain steps hit the original set (jQuery API demo idea).

jQuery
$("div").append(" [border]")
  .add("p")
  .append(" [bg]");
Try It Yourself

How It Works

  1. Select. Start with every div.
  2. Mark border. First .append() runs only on those divs.
  3. Add. .add("p") returns a new set with divs and paragraphs.
  4. Mark bg. The second .append() runs on the whole union.

Example 2 — Official: $("p").add("span")

Merge two tag types, then style once.

jQuery
$("p").add("span").append(" [together]");
Try It Yourself

How It Works

  1. Start. Select all p elements.
  2. Merge. .add("span") includes every span.
  3. One action. A single .append() marks both tags.
  4. Use. Ideal when unrelated elements share the same style step.

Example 3 — Official: List Items Plus a Paragraph

Results sort in document order — list first, then the paragraph.

jQuery
$("li").add("p").append(" [red]");
Try It Yourself

How It Works

  1. Select. Start with every li.
  2. Add. Merge the following p.
  3. Order. Since 1.4, same-document sets sort as they appear on the page.
  4. Mark. All four elements get the same suffix.

Example 4 — Official: Add a DOM Element

Pass a node from getElementById without wrapping it first.

jQuery
$("p").add(document.getElementById("a")).append(" [set]");
Try It Yourself

How It Works

  1. Reference. Hold a plain DOM node (here the span with id a).
  2. Add. .add(element) merges that node into the set.
  3. No wrap. You do not need $(element) first.
  4. Use. Handy when another API already gave you a DOM reference.

Example 5 — Official: Reassign the Collection

Capture the return value — .add() never mutates in place.

jQuery
var collection = $("p");
// collection.add(document.getElementById("a")); // WRONG
collection = collection.add(document.getElementById("a"));
collection.append(" [yes]");
Try It Yourself

How It Works

  1. Store. collection starts as only paragraphs.
  2. Wrong call. Calling .add() without using the return value does nothing useful.
  3. Reassign. Save the new union back into collection.
  4. Act. Now .append() hits paragraphs and the extra node.

Edge Cases & Pitfalls

Common mistakes when growing a jQuery set.

reassign

Forgotten return value

Official warning: pdiv.add("div") alone leaves pdiv unchanged. Chain or reassign.

html

HTML is in memory only

.add("<p>hi</p>") creates nodes but does not show them. Append with .append() / .appendTo().

order

Document order, not arg order

Since 1.4, same-document unions sort by page position. Cross-document order is undefined.

vs append

.add() ≠ .append()

One grows the collection; the other inserts into the DOM. Do not mix the names up.

Browser Support

.add() has been part of jQuery since 1.0. jQuery-object arguments arrived in 1.1; selector + context and document-order sorting in 1.4. It works in every browser that runs your loaded jQuery version.

✓ Stable · jQuery 1.0+

jQuery .add()

Supported across jQuery 1.x, 2.x, and 3.x. Prefer a current jQuery 3.x build. Always capture or chain the returned set.

100% With jQuery
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
.add() Universal

Bottom line: Safe for merging selectors, DOM nodes, and jQuery objects. Remember: new set, document order since 1.4, HTML needs an insert step.

Performance Notes

ApproachCostBest for
.add(element)CheapestKnown DOM node already in hand
.add(selector)OKSmall, scoped pages
.add(sel, context)BetterLimit the search root (1.4+)
One multi-selectorOften enough$("p, span") when both are known upfront

Prefer a single combined selector when you know both sets ahead of time. Use .add() when sets arrive in steps or from different sources.

Key Takeaways

  • Role: build a new union of matched elements.
  • Capture: reassign or chain — never call and discard.
  • Order: document order since 1.4 (same document).
  • Next: .addBack() restores prior set members.

One line: .add() grows the collection, not the DOM — assign the result, then style or insert.

Frequently Asked Questions

.add() creates a new jQuery object that is the union of the current matched elements plus extra elements you pass in — a selector, DOM element(s), HTML snippet, or another jQuery object. Available since jQuery 1.0.
No. Official docs: .add() returns a new set and leaves the original unchanged. Capture it: collection = collection.add(el); or chain directly.
.add() only builds a larger jQuery collection in memory. .append() inserts nodes into the DOM. HTML passed to .add() will not appear on the page until you use an insertion method.
Since jQuery 1.4, when all elements are in the same document, .add() sorts them in document order — not necessarily the order you passed arguments.
Use .not(selector|elements) to drop items from the combined set, or .end() to return to the selection before .add() in a chain.
Anything $() accepts: a selector (1.0), DOM element(s) (1.0), HTML string (1.0), a jQuery object (1.1), or selector plus context (1.4).

Next: .addBack() Method

Learn how to add the previous set of elements back into the chain.

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