jQuery .wrapAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("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>
CallWhat it does
Matched setElements grouped under one parent
wrappingElementHTML / element / jQuery / selector
functionReturns wrapper; this = first match (3.0+)

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the elements that should share one parent.
Step 2Pass HTML, a DOM element, a jQuery object, or a selector as the wrapper.
Step 3Or pass a function (1.4+) that returns the wrapper (once in jQuery 3+).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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

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

.wrapInner() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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