jQuery .wrap() Method

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

What Is .wrap()?

.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
NeedUse
Parent around each match$(els).wrap("<div>")
One parent for the whole set$(els).wrapAll("<div>")
Wrap contents inside each$(els).wrapInner("<span>")
Remove the parent again$(els).unwrap()

How to Use .wrap()

Select the elements, then pass a wrapper structure or a callback.

  1. Select targets: $("p"), $(".item"), etc.
  2. Pass HTML, a DOM element, a jQuery object, or a selector as the wrapper.
  3. Or pass a function (1.4+) that returns a wrapper for each element.
  4. Remember: each match is wrapped separately — use .wrapAll() for one shared parent.
PieceRequired?Role
jQuery setYesElements that receive a parent
wrappingElementYes*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

Cheat Sheet

Keep these facts handy when adding wrappers.

TopicDetail
Syntax.wrap(html|el|jQuery|fn)
ScopeEach matched element, separately
TemplateCloned per match (original not moved)
Nested OKOne innermost target required
vs wrapAllOne shared parent for the set
Inverse.unwrap()
SincejQuery 1.0

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 */

jQuery Examples

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

Example 1 — Basic .wrap()

Wrap each paragraph in its own div.

jQuery
$("p.demo").wrap("<div class='box'></div>");

console.log($("#host").html());
// <div class="box"><p class="demo">Hello</p></div>
// <div class="box"><p class="demo">World</p></div>
Try It Yourself

How It Works

  1. Select. Both p.demo nodes are in the set.
  2. Wrap. jQuery clones <div class="box"> for each.
  3. Place. Each paragraph moves inside its own new parent.
  4. Result. Two wrappers — not one shared container.

Example 2 — .wrap() vs .wrapAll()

See the difference between per-item wrap and one shared parent.

jQuery
$("#a p").wrap("<div>");
$("#b p").wrapAll("<div>");

console.log("wrap:", $("#a").html());
console.log("wrapAll:", $("#b").html());
Try It Yourself

How It Works

  1. wrap. Each p in #a gets its own div.
  2. wrapAll. Both p nodes in #b share one div.
  3. Pick. Per-item styling → wrap; one group → wrapAll.
  4. Rule. Count the wrappers: N matches → N wraps, or 1 wrapAll.

Example 3 — Callback Wrapper (1.4+)

Return a different class for each matched element.

jQuery
$("p.demo").wrap(function (i) {
  return "<div class='item-" + i + "'></div>";
});

console.log($("#host").html());
Try It Yourself

How It Works

  1. Call once per match. Index 0, then 1.
  2. Return HTML. Each return value becomes that element’s wrapper.
  3. this. Inside the function, this is the current DOM element.
  4. Use. Great for unique classes or data-driven wrappers.

Example 4 — Nested Wrapper Structure

Pass a nested HTML string; the element lands in the innermost node.

jQuery
$("#msg").wrap("<div class='outer'><div class='inner'></div></div>");

console.log($("#host").html());
// <div class="outer"><div class="inner"><p id="msg">Hi</p></div></div>
Try It Yourself

How It Works

  1. Structure. Outer and inner divs form the template.
  2. Innermost. #msg is placed inside .inner.
  3. Rule. Nested wrappers are fine if there is one inmost element.
  4. Alt. document.createElement("div") also works as a template.

Example 5 — .wrap() vs .wrapInner()

Outer parent vs wrapping the contents inside the element.

jQuery
$("#w").wrap("<div class='box'></div>");
$("#i").wrapInner("<strong></strong>");

console.log($("#host").html());
Try It Yourself

How It Works

  1. wrap. #w gains an outer div.box parent.
  2. wrapInner. #i stays the outer node; its text moves into <strong>.
  3. Pick. New parent around the element → wrap; wrap children → wrapInner.
  4. 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.

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 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
.wrap() Universal

Bottom line: Safe for all supported jQuery versions. Prefer wrapAll when the whole set should share one parent.

Performance Notes

ApproachCostBest for
.wrap()O(n) wrappersPer-item containers
.wrapAll()One wrapperGroup the whole set
Callback wrapO(n) + fn costDynamic per-item HTML

Cost scales with the number of matched elements because each gets a cloned wrapper. Prefer .wrapAll() when one container is enough.

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.

Next: .wrapAll()

Learn how to wrap the entire set in a single shared parent.

wrapAll() 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