jQuery .wrapAll() Method

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

What Is .wrapAll()?

.wrapAll() wraps all matched elements in one shared parent structure (since jQuery 1.2). Contrast with .wrap(), which wraps each match separately.

Remember
$("p").wrapAll("<div class='group'></div>")
$(".inner").wrapAll(document.createElement("div"))
$("p").wrapAll(function () { return "<section>"; })  // 1.4+

Before:  <p>A</p><p>B</p>
After:   <div class="group"><p>A</p><p>B</p></div>
NeedUse
One parent for the whole set$(els).wrapAll("<div>")
Parent around each match$(els).wrap("<div>")
Wrap contents inside each$(els).wrapInner("<span>")
Remove the parent again$(els).unwrap()

How to Use .wrapAll()

Select the group, then pass one wrapper structure.

  1. Select the elements that should share one parent.
  2. Pass HTML, a DOM element, a jQuery object, or a selector as the wrapper.
  3. Or pass a function (1.4+) that returns the wrapper (once in jQuery 3+).
  4. Keep only one innermost element in nested wrapper HTML.
PieceRequired?Role
Matched setYesElements grouped under one parent
wrappingElementYes*HTML / element / jQuery / selector
functionAlt (1.4+)Returns wrapper; this = first match (3.0+)

Pseudocode

Pseudocode
if argument is a function:
    wrapper = function.call(firstMatchedElement)
else:
    wrapper = clone(wrappingElement)   // templates are cloned, not moved

insert wrapper before the first matched element
move all matched elements into the innermost wrapper node
// non-matched siblings between them stay outside

return original matched set

Cheat Sheet

Keep these facts handy while grouping nodes.

TopicDetail
SincejQuery 1.2 (callback 1.4)
ResultOne shared parent for the whole set
vs wrap.wrap() = one parent each
GapsNon-matched siblings stay outside
TemplatesjQuery/DOM templates are cloned
ReturnOriginal matched set
Inverse.unwrap() on the children

Live Preview

Wrap all sample paragraphs in one parent, or reset.

Browser demo of .wrapAll() — same idea as the examples below.

Hello

cruel

World

Result
Ready — pick an action and Run

jQuery Examples

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

Example 1 — HTML Wrapper

Wrap every paragraph in one new div.

jQuery
$("p").wrapAll("<div></div>");
Try It Yourself

How It Works

  1. Select. Every p is matched.
  2. Build. jQuery creates one wrapper from the HTML string.
  3. Group. All matched paragraphs move into that wrapper.
  4. Return. You still have the original $("p") set.

Example 2 — document.createElement

Pass a real DOM node as the wrapper template.

jQuery
$("p").wrapAll(document.createElement("div"));
Try It Yourself

How It Works

  1. Create. A detached div is built with the DOM API.
  2. Pass. That node is the wrapping element.
  3. Insert. jQuery uses it (or a clone) as the shared parent.
  4. Same idea. Equivalent to passing "<div>".

Example 3 — Non-Matched Gaps Stay Out

Only matched nodes enter the wrapper; neighbors in between do not.

jQuery
$("span").wrapAll("<div></div>");
// text between spans stays outside the new div
Try It Yourself

How It Works

  1. Select. Only span elements match.
  2. Move. Those spans are pulled into the new parent.
  3. Leave. Text/nodes between them stay where they were.
  4. Lesson. .wrapAll() groups the set — not the full sibling range.

Example 4 — Callback Form

Return the wrapper from a function (since 1.4).

jQuery
$("p").wrapAll(function () {
  return "<div class='group'></div>";
});
Try It Yourself

How It Works

  1. Call once (3.0+). The function runs for the whole set.
  2. this. Points at the first matched element.
  3. Return. HTML or a jQuery object becomes the shared wrapper.
  4. Apply. All matches move into that structure.

Example 5 — Clone an Existing Template

Pass a jQuery object — it is cloned, not moved.

jQuery
$("p").wrapAll($(".template"));
// .template stays in place; a clone wraps the paragraphs
Try It Yourself

How It Works

  1. Select template. $(".template") is the pattern.
  2. Clone. jQuery copies that structure for wrapping.
  3. Original stays. The template node is not relocated.
  4. Group. Matched paragraphs sit inside the clone.

Edge Cases & Pitfalls

Check these before grouping large selections.

gaps

In-between siblings are excluded

Only matched elements move into the wrapper. Text or nodes between them stay outside.

clone

jQuery templates are cloned

Passing $(".box") does not move that node — a clone wraps the set.

nest

One innermost element

Nested wrapper HTML is fine, but there must be a single deepest container for the matches.

xss

Untrusted HTML is unsafe

Do not build wrapper strings from URL params, cookies, or form fields without escaping.

Browser Support

.wrapAll() has been part of jQuery since 1.2. The function callback arrived in 1.4; jQuery 3.0 fixed it to run once for the set.

✓ Stable · jQuery 1.2+

jQuery .wrapAll()

Supported in all browsers that run jQuery 1.2+. Prefer a current jQuery 3.x build for modern projects.

100% jQuery core method
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
.wrapAll() Universal

Bottom line: Use when the whole selection should share one parent. Prefer .wrap() when each match needs its own.

Performance Notes

ApproachCostBest for
Small set + one wrapAllCheapGrouping a short list
Deep nested wrapper HTMLModerateOccasional layout shells
Huge selections oftenExpensiveAvoid — restructure less often

One shared wrapper is cheaper than wrapping each node separately with .wrap() when you only need a single group container.

Key Takeaways

  • Role: one shared parent for the entire matched set.
  • vs wrap: .wrap() = parent per element.
  • Gaps: non-matched siblings stay outside the group.
  • Undo: .unwrap() removes the shared parent.

One line: .wrapAll() puts the whole selection under one parent — use it when you need a group container, not per-item wrappers.

Frequently Asked Questions

.wrapAll() wraps all matched elements together in one shared parent structure. Returns the original matched set. Available since jQuery 1.2; callback form since 1.4.
.wrap() gives each match its own parent. .wrapAll() puts the whole set inside one shared parent.
.wrapAll() wraps the matched elements themselves. .wrapInner() wraps the contents inside each matched element.
Non-matched nodes between selected elements are left out of the wrapper. Only the matched set is grouped.
No. When you pass a jQuery object or DOM node as the wrapper template, jQuery clones that structure. The original stays put.
Yes, since 1.4. In jQuery 3.0+, the callback runs once with this set to the first matched element and should return HTML or a jQuery object.
Call .unwrap() on the wrapped children to remove the shared parent and leave them in place.

Next: .wrapInner()

Wrap the contents inside each matched element while keeping the outer tag.

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

8 people found this page helpful