.wrap() inserts a parent structure around each matched element (since jQuery 1.0). Every match gets its own copy of the wrapper. Returns the original jQuery set. Inverse: .unwrap().
Remember
$(els).wrap("<div class='box'></div>")
$(els).wrap(function (i) { return "<div class='n" + i + "'>"; })
Before: <p>A</p><p>B</p>
After: <div><p>A</p></div><div><p>B</p></div>
.wrap() → each element gets its own parent
.wrapAll() → one parent for the whole set
.wrapInner()→ wrap contents inside the element
Select the elements, then pass a wrapper structure or a callback.
Select targets: $("p"), $(".item"), etc.
Pass HTML, a DOM element, a jQuery object, or a selector as the wrapper.
Or pass a function (1.4+) that returns a wrapper for each element.
Remember: each match is wrapped separately — use .wrapAll() for one shared parent.
Piece
Required?
Role
jQuery set
Yes
Elements that receive a parent
wrappingElement
Yes*
HTML / element / jQuery / selector
function(index)
Alt (1.4+)
Return wrapper per element
Return
—
Original matched set
Since
—
1.0; callback in 1.4
Pseudocode
Pseudocode
wrap(wrapperOrFn):
for each element in this set:
structure = copy of wrapper
(or result of fn(index) for this element)
insert structure where element is
place element inside the innermost node of structure
return this set
// structure may be nested, but must have one innermost element
Reference
Cheat Sheet
Keep these facts handy when adding wrappers.
Topic
Detail
Syntax
.wrap(html|el|jQuery|fn)
Scope
Each matched element, separately
Template
Cloned per match (original not moved)
Nested OK
One innermost target required
vs wrapAll
One shared parent for the set
Inverse
.unwrap()
Since
jQuery 1.0
Try it
Live Preview
Wrap a small HTML sketch and compare wrap, wrapAll, and callback forms.
Browser demo of .wrap() — same ideas as the examples below.
Result
after → <div><p>A</p></div><div><p>B</p></div>
/* each <p> got its own parent */
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
wrapInner.#i stays the outer node; its text moves into <strong>.
Pick. New parent around the element → wrap; wrap children → wrapInner.
Inverse of wrap. Use .unwrap() to strip that outer parent later.
Edge Cases & Pitfalls
Check these before wrapping elements in production.
each
Each match gets its own wrapper
Three paragraphs → three parents. Need one shared parent? Use .wrapAll().
text
Text between siblings is not wrapped
Only matched elements move into the new parent. Loose text between spans stays outside.
clone
Templates are cloned
Passing a jQuery object clones it. The original template node is not moved from the page.
nested
One innermost element
Nested wrappers must have a single deepest element — that is where the match is placed.
Compatibility
Browser Support
.wrap() needs jQuery 1.0+. The callback form needs jQuery 1.4+.
✓ jQuery 1.0+
.wrap()
Works in Chrome, Firefox, Safari, and Edge when jQuery is loaded. Use 1.4+ for the function callback signature.
100%With jQuery 1.0+
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
.wrap()Universal
Bottom line: Safe for all supported jQuery versions. Prefer wrapAll when the whole set should share one parent.
Analysis
Performance Notes
Approach
Cost
Best for
.wrap()
O(n) wrappers
Per-item containers
.wrapAll()
One wrapper
Group the whole set
Callback wrap
O(n) + fn cost
Dynamic per-item HTML
Cost scales with the number of matched elements because each gets a cloned wrapper. Prefer .wrapAll() when one container is enough.
Remember
Key Takeaways
Role: add a parent around each matched element.
Family: wrap / wrapAll / wrapInner / unwrap.
Callback: return a custom wrapper per index (1.4+).
Safety: never pass untrusted HTML into .wrap().
One line:.wrap() gives every matched element its own new parent.
Frequently Asked Questions
.wrap() inserts a parent structure around each matched element. Each match gets its own copy of the wrapper. Returns the original jQuery set. Available since jQuery 1.0; callback form since 1.4.
.wrap() wraps each matched element separately. .wrapAll() wraps the whole set in one shared parent.
.wrap() puts a new parent around the matched element. .wrapInner() wraps the contents inside the matched element and leaves the element itself as the outer node.
.unwrap() removes the immediate parent of matched elements and leaves those elements in place.
Yes, since jQuery 1.4. The function receives the index, this is the current element, and it should return an HTML string, DOM element, or jQuery object used as the wrapper.
No. When you pass a jQuery object or DOM node as the template, jQuery clones that structure for each match. The original template stays where it is.