The .add() traversing method builds a new jQuery object from the union of your current matched elements and additional ones — selected by CSS selector, DOM reference, HTML string, or another jQuery collection. This tutorial follows the official jQuery API demos for styling combined sets, document order, and correct reassignment.
01
Syntax
.add(sel)
02
Union
Merge sets
03
New object
Non-destructive
04
Doc order
Since 1.4
05
Chain
.css() etc.
06
.not()
Reverse add
Fundamentals
Introduction
jQuery selections often start narrow — all paragraphs, or every list item. Real pages need to target multiple unrelated groups at once: highlight every <div>and every <p>, or style list items together with a footer paragraph.
Instead of running the same .css() call twice, you combine collections with .add(). It accepts the same inputs as $() — selectors, DOM nodes, HTML fragments, and jQuery objects — and returns a fresh jQuery object you can keep chaining.
Concept
Understanding the .add() Method
Given a jQuery object representing a set of DOM elements, .add() constructs a new jQuery object from the union of those elements and whatever you pass in. Duplicates are removed automatically — each DOM node appears at most once in the result.
Since jQuery 1.4, when every element lives in the same document, the combined set is sorted in document order (the order nodes appear in the HTML tree), not necessarily the order you listed selectors. To undo a union, use .not() to subtract elements or .end() to step back in a chain.
💡
Beginner Tip
.add() builds a collection — it does not insert HTML into the page. To place new nodes on screen, chain an insertion method like .appendTo() after creating elements.
Foundation
📝 Syntax
General forms of .add:
jQuery
.add( selector )
.add( elements )
.add( html )
.add( selection )
.add( selector, context )
Parameters
selector — a string containing a selector expression to find additional elements (jQuery 1.0+).
elements — one or more DOM elements to include (jQuery 1.0+).
html — an HTML fragment; jQuery creates matching elements and adds them to the set (jQuery 1.0+). They exist in memory until you append them.
selection — an existing jQuery object whose elements are merged in (jQuery 1.1+).
selector, context — search for selector starting from context, like $(selector, context) (jQuery 1.4+).
Return value
A new jQuery object containing the combined, de-duplicated elements — sorted in document order when possible.
Official jQuery API pattern
jQuery
$( "p" ).add( "div" ).addClass( "widget" );
var pdiv = $( "p" ).add( "div" ); // save the union
Cheat Sheet
⚡ Quick Reference
Goal
Code
Add by selector
$("li").add("p")
Add DOM element
$("p").add(element)
Add with context
$(".a").add(".b", "#wrap")
Style the union
$("div").add("p").css("background", "yellow")
Save result
collection = collection.add(el)
Undo union
.not(selector) or .end()
Compare
📋 .add() vs .not() vs .append()
Three different “add” concepts in jQuery.
.add()
union set
Merge into a new jQuery object
.not()
subtract
Remove elements from the set
.end()
go back
Previous selection before .add()
.append()
insert DOM
Put nodes inside a parent on the page
Hands-On
Examples Gallery
Examples 1–2 match the official jQuery API documentation demos. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Combine selectors and style the union — official jQuery demos.
Example 1 — Official Demo: Divs and Paragraphs
Border every div, then add all p elements and give the combined set a yellow background — the primary jQuery API example.
All div elements get a red border.
All div and p elements get a yellow background.
(Paragraph added via .add() shows yellow but no red border.)
How It Works
The chain first styles only div nodes with a border. .add("p") returns a new object containing every div and p, so the final .css("background") applies to both — including the note in the official demo that the new paragraph gets yellow fill but not the border step.
📈 Practical Patterns
More selectors, list + paragraph unions, DOM references, and reassignment.
Example 2 — Official Demo: Paragraphs and Spans
Add every span to the current p selection and highlight both with a yellow background.
li and p are unrelated in the DOM tree, but .add() still merges them. jQuery sorts the result in document order — list items first, then the paragraph below the list.
Example 4 — Add a DOM Element by Reference
Pass a native DOM node instead of a selector — official Example 4 from the jQuery API.
All p elements plus #footer-note get yellow background.
How It Works
When you already hold a DOM reference — from getElementById, an event target, or an iframe document — pass it directly to .add() without wrapping it in $() first.
Example 5 — Reassign the Collection (Common Mistake)
Official Example 5: .add() returns a new object — you must capture it.
jQuery
var collection = $( "p" );
// WRONG — original collection unchanged:
// collection.add( document.getElementById( "footer-note" ) );
// RIGHT — capture the new union:
collection = collection.add( document.getElementById( "footer-note" ) );
collection.css( "background", "yellow" );
After reassignment, p elements + #footer-note are styled yellow.
Without reassignment, only the original p set would be styled.
How It Works
jQuery traversing methods follow an immutable-set pattern: the original variable still points at the old object. Assign the return value of .add() back to your variable — or chain directly without storing intermediate results.
Applications
🚀 Common Use Cases
Batch styling — apply one .css() or .addClass() to headings and paragraphs together.
Form fields — combine input, select, and textarea for validation plugins.
Event binding — attach one .on("click") handler to buttons and links in different containers.
Animation groups — fade unrelated panels as one jQuery collection.
Plugin APIs — let callers pass extra nodes via .add(extra) before initialization.
Testing counts — verify total selected nodes after merging sets with .length.
🧠 How .add() Builds a Union
1
Start with a set
A jQuery object already holds zero or more matched DOM nodes.
Current
2
Resolve new input
jQuery interprets selector, DOM node, HTML, or jQuery object like $().
Input
3
Merge & dedupe
Union both groups; duplicate nodes appear once; sort in document order.
Union
4
🔌
Return new object
Chain .css(), .on(), or save to a variable for later.
Important
📝 Notes
Available since jQuery 1.0; document-order sorting guaranteed since jQuery 1.4.
Always capture the return value — .add() does not modify the original jQuery object in place.
HTML passed to .add() creates elements in memory; they are not visible until you append them.
Reverse with .not() to subtract, or .end() to revert a chain step.
Do not confuse with .append(), which inserts content into the DOM tree.
Elements from different documents may not have a defined sort order.
Compatibility
Browser Support
.add() has been part of jQuery since 1.0+. Context argument support arrived in 1.4. It works wherever jQuery runs — all modern browsers and IE with supported jQuery versions.
✓ jQuery 1.0+
jQuery .add()
Supported in jQuery 1.x, 2.x, and 3.x across all browsers that run your chosen jQuery build. No native DOM equivalent — use jQuery or manually merge NodeLists.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.add()Universal
Bottom line: Safe in any jQuery project. Prefer chaining or reassignment over expecting in-place mutation of jQuery collections.
Wrap Up
Conclusion
The jQuery .add() method merges your current matched elements with additional nodes found by selector, DOM reference, HTML, or another jQuery object. It returns a new collection sorted in document order — ideal for applying one operation to unrelated groups on the page.
Remember the official API lesson: chain .add() directly, or assign its return value to a variable. Use .not() or .end() when you need to narrow the set again.
Scope selectors with .add(sel, context) when needed
Check .length after merging collections
❌ Don’t
Call .add() without using the return value
Expect HTML added via .add() to appear on the page automatically
Confuse .add() with .append() or .after()
Assume argument order equals result order — document order wins
Merge huge unrelated sets when a single scoped selector would suffice
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .add()
Union sets, chain forward.
5
Core concepts
+01
.add()
Union sets
API
🔌02
New object
Reassign
Return
📄03
Doc order
Since 1.4
Sort
🔍04
Selector
DOM, HTML
Input
↺05
.not()
Reverse
Pair
❓ Frequently Asked Questions
.add() creates a new jQuery object containing the union of the current matched elements plus any additional elements you pass in — via selector string, DOM element, HTML snippet, or another jQuery object. It does not mutate the original collection unless you reassign the variable.
No. .add() returns a new jQuery object. Calling pdiv.add('div') without assignment leaves pdiv unchanged. Capture the result: var combined = $('p').add('div');
A CSS selector string, one or more DOM elements, an HTML string (since 1.0), another jQuery object (since 1.1), or a selector plus context element (since 1.4) — the same kinds of input $() accepts.
Since jQuery 1.4, when all elements share the same document, .add() returns them sorted in document order — the order they appear in the page — not necessarily the order you passed arguments.
.add() builds a new jQuery collection in memory for further chaining — it does not insert nodes into the DOM. .append() moves or copies nodes into a parent element on the page.
Use .not(selector) or .not(elements) to remove items from the combined set, or .end() to return to the previous selection before .add() was called in a chain.
Did you know?
Before jQuery 1.4, .add() simply concatenated sets — order could surprise you. The 1.4 change sorted results in document order whenever all nodes share one document, which is why $("li").add("p") reliably highlights list items before the paragraph below them, matching how readers see the page.