jQuery .wrap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

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

jQuery
$(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
CallWhat it does
jQuery setElements that receive a parent
wrappingElementHTML / element / jQuery / selector
function(index)Return wrapper per element
ReturnOriginal matched set
Since1.0; callback in 1.4

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

Parameters

ParameterDescription
indexUsed in function(index). Return wrapper per element

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: Traversing Methods

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

Traversing Methods 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.

9 people found this page helpful