jQuery .wrapAll() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM methods

What You’ll Learn

The .wrapAll() method inserts a single new parent around the entire matched collection — all siblings move together into one wrapper. This tutorial covers syntax since 1.2, callback form since 1.4, comparisons with .wrap(), .wrapInner(), and .unwrap(), the official span demo, and five practical try-it examples.

01

.wrapAll()

One wrapper

02

All matches

Single group

03

vs .wrap()

Per element

04

Callback

Once only

05

Clone

Not moved

06

Since 1.2

Core API

Introduction

Sometimes you need to group sibling elements under one container — a <div> around a set of paragraphs, a fieldset around related inputs, or a semantic wrapper for a block of inline spans. When every matched element should share the same parent, jQuery provides .wrapAll().

Available since jQuery 1.2, .wrapAll() inserts one wrapper around the entire matched collection. Pass an HTML string, selector, DOM element, or jQuery object. Since jQuery 1.4, a callback can return a custom wrapper — the function runs once, and this refers to the first matched element. This differs from .wrap(), which wraps each element separately.

Text between sibling inline elements is not included when wrapping spans. jQuery clones DOM nodes and jQuery objects rather than moving the original template. Browse the jQuery DOM hub for related manipulation methods.

Understanding the .wrapAll() Method

.wrapAll( wrappingElement ) or .wrapAll( function ) creates one new parent around the entire jQuery collection. All matched nodes move together inside that single wrapper — three paragraphs wrapped with .wrapAll( "<div>" ) produce one <div><p>…</p><p>…</p><p>…</p></div> group.

The return value is the same jQuery collection (for chaining). When you pass a DOM node or jQuery object, jQuery clones it once rather than moving the original. The callback form runs once for the whole set; prior to jQuery 3.0 it was incorrectly called per element.

💡
Beginner Tip

Before: <p>One</p><p>Two</p>. After $("p").wrapAll( "<div class='group'>" ): <div class="group"><p>One</p><p>Two</p></div>. Compare with $("p").wrap( "<div>" ), which would create two separate div wrappers.

📝 Syntax

jQuery .wrapAll() supports two forms:

Wrap all matches with HTML string, selector, element, or jQuery object — since 1.2

jQuery
.wrapAll( wrappingElement )
  • wrappingElement — HTML string, selector, DOM element, or jQuery object.
  • All matched elements move together into one shared wrapper.
  • DOM elements and jQuery objects are cloned, not moved from their source.

Wrap all matches with callback — since 1.4

jQuery
.wrapAll( function )
  • Callback runs once for the entire collection (fixed in jQuery 3.0).
  • this inside the function refers to the first matched DOM element.
  • Return an HTML string or jQuery object to use as the shared wrapper.

Return value

  • Returns the same jQuery collection that was wrapped.
  • Chain further manipulation on the wrapped elements.

Official wrapAll paragraph demo

The jQuery API documentation wraps all paragraphs in a single div:

jQuery
$( "p" ).wrapAll( "<div>" );



// CSS for the official demo:

// div { border: 1px solid blue; }

⚡ Quick Reference

GoalCode
Wrap all matches in one div$("p").wrapAll("<div>")
Wrap all with styled container$("p").wrapAll("<div class='group'>")
Wrap each match separately$("p").wrap("<div>")
Dynamic shared wrapper via callback$("p").wrapAll(function(){ return "<div class='batch'>"; })
Wrap contents inside each element$("p").wrapInner("<span>")
Remove wrapper (inverse)$("p").unwrap()

📋 .wrapAll() vs .wrap() vs .wrapInner() vs .unwrap()

Four related DOM structure APIs — pick the right wrapping strategy.

.wrapAll()
one wrapper

Wrap the entire collection together in a single shared parent

.wrap()
per element

Insert a separate wrapper around each matched element

.wrapInner()
wrap contents

Wrap children inside the matched element, not the element itself

.unwrap()
remove parent

Strip the immediate parent — inverse of wrap and wrapAll

Examples Gallery

Examples 1–5 follow the official jQuery API documentation and common production patterns. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery div wrapAll demo and wrap vs wrapAll contrast.

Example 1 — Official Demo: Wrap All Paragraphs in One Div

Wrap every paragraph in a single shared <div> with a blue border — the classic starting point from the jQuery API docs.

jQuery
$( "p" ).wrapAll( "<div>" );



// CSS for the official demo:

// div { border: 1px solid blue; }
Try It Yourself

How It Works

.wrapAll( "<div>" ) inserts one new div parent around the entire matched set. Every paragraph moves inside that single container. Pair with .unwrap() to remove the shared wrapper.

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

Three paragraphs illustrate the key difference — separate wrappers versus one shared wrapper.

jQuery
// .wrap() — three separate divs

$( "#a p" ).wrap( "<div class='each'>" );

// Result: <div class="each"><p>…</p></div> × 3



// .wrapAll() — one shared div

$( "#b p" ).wrapAll( "<div class='group'>" );

// Result: <div class="group"><p>…</p><p>…</p><p>…</p></div>
Try It Yourself

How It Works

.wrap() clones the wrapper for every match. .wrapAll() creates one wrapper and moves the entire collection inside it. This is the most common source of confusion between the two APIs.

📈 Practical Patterns

Inline span behavior, callback wrappers, and DOM element cloning.

Example 3 — Span wrapAll: Text Between Siblings Left Out

When wrapping inline spans together, text nodes between matched siblings are not included in the wrapper — official jQuery API demo.

jQuery
// HTML: Hello <span>John</span> and <span>Paul</span>!

$( "span" ).wrapAll( "<div></div>" );



// Result: Hello <div><span>John</span><span>Paul</span></div>!

// "and" stays outside the wrapper
Try It Yourself

How It Works

.wrapAll() operates on matched elements only. Sibling text nodes and other inline content stay in place. Unlike .wrap(), which would create separate divs around each span, wrapAll groups both spans under one parent while leaving intervening text outside.

Example 4 — Callback wrapAll With Custom Wrapper

Return a shared wrapper using a function — since jQuery 1.4. The callback runs once; this is the first matched element.

jQuery
$( "p" ).wrapAll( function() {

  var count = $( this ).parent().find( "p" ).length;

  return "<div class='batch count-" + count + "'></div>";

});



// All paragraphs share one div whose class reflects the batch

// this === first matched <p> element in the collection
Try It Yourself

How It Works

Unlike .wrap(), where the callback runs per element, .wrapAll() evaluates the function once for the entire set. Use this or the collection context to derive wrapper attributes for the group.

Example 5 — wrapAll With document.createElement or jQuery Object

Pass a DOM node or jQuery collection — jQuery clones the structure once without moving the original.

jQuery
// Using document.createElement

var wrapper = document.createElement( "div" );

wrapper.className = "group";

$( "p" ).wrapAll( wrapper );



// Using a jQuery object — also cloned, not moved

var $wrapper = $( "<div class='group'></div>" );

$( "p" ).wrapAll( $wrapper );



// Original wrapper variable stays in memory; one clone wraps the set
Try It Yourself

How It Works

Unlike .append(), which moves nodes, .wrapAll() clones the supplied element once and wraps the entire collection. Reuse one template variable to group many siblings without it disappearing from the DOM.

🚀 Common Use Cases

  • Sibling grouping — wrap a set of paragraphs, list items, or table rows in one container for layout or styling.
  • Form fieldsets — group related inputs under one fieldset or div without wrapping each input separately.
  • Plugin batch setup — wrap a collection of elements in the single parent structure a third-party script expects.
  • Inline element batches — group multiple spans or links under one parent while leaving intervening text untouched.
  • Dynamic batch wrappers — callback form assigns one shared class or data attribute based on the first matched element.
  • CMS cleanup — re-group scattered sibling blocks under one semantic wrapper during content normalization.

🧠 How .wrapAll() Groups Matched Elements

1

Match target elements

jQuery collection points at nodes that will share one new parent.

Select
2

Build wrapper structure

Parse HTML string, clone DOM element, or evaluate callback return value once.

Clone
3

Insert single wrapper

One parent is placed around the first matched element in document order.

One parent
4

Move entire collection

All matched nodes nest inside the shared wrapper; collection returned for chaining.

📝 Notes

  • Available since jQuery 1.2 — callback form added in jQuery 1.4.
  • Wraps all matched elements together in one shared wrapper (use .wrap() for separate copies).
  • Callback runs once for the collection; this refers to the first matched element (fixed in jQuery 3.0).
  • Text between sibling inline elements is not wrapped — only matched nodes move.
  • DOM elements and jQuery objects are cloned, not moved from their source location.
  • Use .unwrap() to remove the shared parent wrapper.
  • Do not confuse with .wrapInner(), which wraps contents inside each matched element.

Browser Support

.wrapAll() has been part of jQuery since 1.2+, with the callback form since 1.4+. It relies on standard DOM parent-child insertion. Works in all browsers supported by your jQuery build — IE6+ through modern evergreen browsers in legacy jQuery versions, and all current browsers in jQuery 3.x and 4.x.

jQuery 1.2+

jQuery .wrapAll()

Universal DOM group-wrapping API across jQuery 1.x, 2.x, 3.x, and 4.x. No browser-specific polyfills required — jQuery normalizes wrapper insertion and cloning. Pair with .unwrap() to remove the shared wrapper.

100% With jQuery loaded
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: Default choice for grouping all matched siblings under one parent wrapper.

Conclusion

The jQuery .wrapAll() method inserts a single new parent around the entire matched collection. Pass an HTML string, DOM node, jQuery object, or callback. All siblings move together into one shared wrapper — the opposite of wrapping each element separately with .wrap().

Choose .wrap() for per-element wrappers, .wrapInner() to wrap contents inside each element, and .unwrap() to remove the shared parent. Next up: inserting content with .wrapInner().

💡 Best Practices

✅ Do

  • Use .wrapAll() when siblings should share one container
  • Pair with .unwrap() to remove the shared wrapper later
  • Use .wrap() when each match needs its own wrapper copy
  • Remember the callback runs once — this is the first match
  • Clone reusable templates with document.createElement or jQuery objects

❌ Don’t

  • Use .wrapAll() when each element needs a separate wrapper
  • Expect text between inline siblings to be included in the wrapper
  • Assume a passed DOM element is moved — it is cloned once
  • Confuse .wrapAll() with .wrapInner() or .wrap()
  • Expect the callback to run per element (pre-3.0 bug; fixed in modern jQuery)

Key Takeaways

Knowledge Unlocked

Five things to remember about .wrapAll()

The group-together API — one shared wrapper for the entire collection.

5
Core concepts
02

vs wrap

Per element

Compare
03

.unwrap()

Remove

Pair
ƒ 04

Callback

Once only

Since 1.4
05

Text gap

Excluded

Spans

❓ Frequently Asked Questions

.wrapAll() inserts a single new parent element around the entire matched collection — all matched nodes move together into one shared wrapper. It returns the same jQuery collection. Available since jQuery 1.2 for HTML strings, selectors, DOM elements, and jQuery objects; the callback form since 1.4. Use .unwrap() to remove the shared wrapper.
.wrap() wraps each matched element separately — three paragraphs become three wrapped groups. .wrapAll() wraps the entire collection together — three paragraphs share one wrapper. Use wrap for per-item styling; use wrapAll when siblings should move into a single container.
.wrapAll() places all matched elements inside one new outer parent. .wrapInner() keeps each matched element as the outer node and wraps its contents instead. Use wrapAll to group siblings under one div; use wrapInner to wrap text inside each paragraph without adding an outer parent around the p tag itself.
No. When wrapping inline elements like spans, only the matched nodes are wrapped together. Text nodes between siblings stay outside the new wrapper. The official jQuery API demo shows Hello [span] and [span] world — the word "and" between spans is not included in the wrapper.
Since jQuery 1.4, pass a function that returns the wrapper HTML or jQuery object. The callback runs once for the entire collection — not once per element. Inside the function, this refers to the first matched DOM element. Prior to jQuery 3.0, the callback was incorrectly invoked per element; modern jQuery calls it once.
Yes. Pass an HTML string, selector, DOM element, or jQuery object. jQuery clones the structure once and wraps the entire collection — the original node is not moved from its source location. Use document.createElement or $("<div>") when you need a reusable template.
Did you know?

jQuery added .wrapAll() in version 1.2 — two minor releases after .wrap() in 1.0 — specifically to address the need for grouping siblings under one parent. The callback form arrived in 1.4 alongside .wrap()’s callback. A long-standing bug caused the callback to run per element before jQuery 3.0; modern jQuery correctly invokes it once with this pointing at the first match. The official span demo exists to show that text between inline siblings is left untouched even when all spans share one wrapper.

Next: jQuery .wrapInner() Method

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.

6 people found this page helpful