.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().
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$(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
Call
What it does
jQuery set
Elements that receive a parent
wrappingElement
HTML / element / jQuery / selector
function(index)
Return wrapper per element
Return
Original matched set
Since
1.0; callback in 1.4
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
index
Used in function(index). Return wrapper per element
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
Return HTML. Each return value becomes that element’s wrapper.
this. Inside the function, this is the current DOM element.
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.