Example 1 — HTML Wrapper
Wrap every paragraph in one new div.
$("p").wrapAll("<div></div>"); How It Works
- Select. Every
pis matched. - Build. jQuery creates one wrapper from the HTML string.
- Group. All matched paragraphs move into that wrapper.

.wrapAll() wraps all matched elements in one shared parent structure (since jQuery 1.2). Contrast with .wrap(), which wraps each match separately.
Start with these common calls. Keep them short and readable.
$("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> | Call | What it does |
|---|---|
Matched set | Elements grouped under one parent |
wrappingElement | HTML / element / jQuery / selector |
function | Returns wrapper; this = first match (3.0+) |
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
| Parameter | Description |
|---|---|
| Step 1 | Select the elements that should share one parent. |
| Step 2 | Pass HTML, a DOM element, a jQuery object, or a selector as the wrapper. |
| Step 3 | Or pass a function (1.4+) that returns the wrapper (once in jQuery 3+). |
| Return Value | Description |
|---|---|
| jQuery object | Most setters and traversing helpers return the jQuery collection so you can chain more methods. |
Wrap every paragraph in one new div.
$("p").wrapAll("<div></div>"); p is matched.document.createElementPass a real DOM node as the wrapper template.
$("p").wrapAll(document.createElement("div")); div is built with the DOM API.Only matched nodes enter the wrapper; neighbors in between do not.
$("span").wrapAll("<div></div>");
// text between spans stays outside the new div span elements match.Wrap the contents inside each matched element while keeping the outer tag.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful