jQuery .wrap() Method

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

What You’ll Learn

The .wrap() method inserts a new parent element around each matched node — the inverse of .unwrap(). This tutorial covers syntax since 1.0, callback form since 1.4, nested wrapper structures, comparisons with .wrapInner(), .wrapAll(), and .unwrap(), the official blue-border demo, and five practical try-it examples.

01

.wrap()

Add parent

02

Per element

Separate copy

03

.unwrap()

Inverse

04

Callback

Since 1.4

05

Nested

Multi-layer

06

Since 1.0

Core API

Introduction

Sometimes you need to add a container around existing elements — a <div> for styling, a semantic wrapper for accessibility, or a plugin that expects inputs inside a specific parent. When the matched elements should stay but gain a new parent, jQuery provides .wrap().

Available since jQuery 1.0, .wrap() inserts a wrapper around each matched element separately. Pass an HTML string, selector, DOM element, or jQuery object. Since jQuery 1.4, a callback can return a custom wrapper per element. This is the opposite of .unwrap(), which removes the immediate parent and promotes children up one level.

The wrapping structure must contain exactly one innermost element where the matched node is placed; outer layers can be nested. Text between sibling inline elements is not included when wrapping spans. Browse the jQuery DOM hub for related manipulation methods.

Understanding the .wrap() Method

.wrap( wrappingElement ) or .wrap( function ) creates a new parent around each matched element. jQuery clones the wrapper structure for every match — three paragraphs wrapped with .wrap( "<div>" ) produce three separate <div><p>…</p></div> groups.

The return value is the same jQuery collection (for chaining). The wrapper HTML must have a single innermost element; nested markup like <div class="outer"><div class="inner"></div></div> is valid. When you pass a DOM node or jQuery object, jQuery clones it rather than moving the original.

💡
Beginner Tip

Before: <p>Hello</p>. After $("p").wrap( "<div class='box'>" ): <div class="box"><p>Hello</p></div>. Compare with $("p").wrapInner( "<span>" ), which wraps content inside the paragraph instead of adding an outer parent around it.

📝 Syntax

jQuery .wrap() supports two forms:

Wrap with HTML string, selector, element, or jQuery object — since 1.0

jQuery
.wrap( wrappingElement )
  • wrappingElement — HTML string, selector, DOM element, or jQuery object.
  • Structure must have one innermost element; can include nested outer layers.
  • Each matched element gets its own copy of the wrapper.

Wrap with callback — since 1.4

jQuery
.wrap( function )
  • Callback receives the index and the DOM element; return HTML string or jQuery object.
  • Use for dynamic wrappers — e.g. a class derived from element text.

Return value

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

Official wrap/unwrap toggle

The jQuery API documentation demo toggles a div wrapper around paragraphs:

jQuery
var pTags = $( "p" );
$( "button" ).on( "click", function() {
  if ( pTags.parent().is( "div" ) ) {
    pTags.unwrap();
  } else {
    pTags.wrap( "<div>" );
  }
});

⚡ Quick Reference

GoalCode
Wrap each match in a div$("p").wrap("<div>")
Wrap with styled container$("p").wrap("<div class='box'>")
Nested wrapper structure$(".inner").wrap("<div class='outer'><div class='mid'></div></div>")
Dynamic wrapper via callback$("span").wrap(function(){ return "<div class='" + $(this).text() + "'>"; })
Wrap all matches in one container$("p").wrapAll("<div>")
Remove wrapper (inverse)$("p").unwrap()

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

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

.wrap()
add parent

Insert a wrapper around each matched element separately

.wrapInner()
wrap contents

Wrap children inside the matched element, not the element itself

.wrapAll()
one wrapper

Wrap the entire collection together in a single parent

.unwrap()
remove parent

Strip the immediate parent — inverse of wrap

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 wrap demo and nested wrapper structures.

Example 1 — Official Demo: Wrap Paragraphs in a Div

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

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

// CSS for the official demo:
// div { border: 1px solid blue; }
Try It Yourself

How It Works

.wrap( "<div>" ) inserts a new div parent around every matched paragraph. Each match receives a separate copy of the wrapper. Pair with .unwrap() to toggle the wrapper off again.

Example 2 — Nested Wrapper Structure

Wrap elements in a multi-layer container — the structure can nest outer and inner divs around each match.

jQuery
$( ".inner" ).wrap(
  "<div class='container'><div class='inner'></div></div>"
);

// Before: <div class="inner">Content</div>
// After:  <div class="container">
//           <div class="inner"><div class="inner">Content</div></div>
//         </div>
Try It Yourself

How It Works

jQuery parses the HTML string and places each matched element inside the innermost empty element. Outer nested tags become ancestors. Useful for layout shells that need both an outer grid cell and an inner content frame.

📈 Practical Patterns

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

Example 3 — Span Wrap: Text Between Siblings Left Out

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

jQuery
// HTML: Hello <span>John</span> and <span>Paul</span>!
$( "span" ).wrap( "<div></div>" );

// Result: Hello <div><span>John</span></div> and
//         <div><span>Paul</span></div>!
// "and" stays outside both wrappers
Try It Yourself

How It Works

.wrap() operates on matched elements only. Sibling text nodes and other inline content stay in place. This differs from .wrapAll(), which would group all spans under one parent and pull intervening text along.

Example 4 — Callback Wrap With Class From Text

Return a custom wrapper per element using a function — since jQuery 1.4.

jQuery
$( "span" ).wrap( function() {
  var className = $( this ).text();
  return "<div class='" + className + "'></div>";
});

// span text "John" → <div class="John"><span>John</span></div>
// span text "Paul" → <div class="Paul"><span>Paul</span></div>
Try It Yourself

How It Works

The callback receives the zero-based index and the raw DOM element. Return value becomes the wrapper for that specific match. Ideal when wrapper attributes depend on element data, position, or content.

Example 5 — Wrap With document.createElement or jQuery Object

Pass a DOM node or jQuery collection — jQuery clones the structure for each match without moving the original.

jQuery
// Using document.createElement
var wrapper = document.createElement( "div" );
wrapper.className = "box";
$( "p" ).wrap( wrapper );

// Using a jQuery object — also cloned, not moved
var $wrapper = $( "<div class='box'></div>" );
$( "p" ).wrap( $wrapper );

// Original wrapper variable stays in memory; clones used per match
Try It Yourself

How It Works

Unlike .append(), which moves nodes, .wrap() clones the supplied element for each match. Reuse one template variable across many elements without it disappearing from the DOM after the first wrap.

🚀 Common Use Cases

  • Visual grouping — add bordered or shaded divs around paragraphs, cards, or list items for layout.
  • Plugin requirements — wrap inputs or images in the container structure a third-party script expects.
  • Toggle editing modes — wrap content for preview, unwrap for plain editing (pair with .unwrap()).
  • Nested layout shells — insert outer grid cells and inner content frames in one .wrap() call.
  • Dynamic styling — callback form assigns per-element classes or data attributes on the wrapper.
  • Inline element isolation — wrap spans or links individually without affecting intervening text nodes.

🧠 How .wrap() Inserts Parent Wrappers

1

Match target elements

jQuery collection points at nodes that will receive a new parent.

Select
2

Build wrapper structure

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

Clone
3

Insert per match

Each matched element gets its own copy of the wrapper placed around it.

Per element
4

Nest matched node

Element moves inside the innermost wrapper slot; collection returned for chaining.

📝 Notes

  • Available since jQuery 1.0 — callback form added in jQuery 1.4.
  • Wraps each matched element separately (use .wrapAll() for one shared wrapper).
  • Inverse of .unwrap() — the official demo toggles both methods.
  • Wrapper HTML must contain exactly one innermost element; nested outer tags are allowed.
  • 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.
  • Do not confuse with .wrapInner(), which wraps contents inside the matched element.

Browser Support

.wrap() has been part of jQuery since 1.0+, 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.0+

jQuery .wrap()

Universal DOM 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() for reversible wrapper toggles.

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

Bottom line: Default choice for adding one wrapper layer around each matched element.

Conclusion

The jQuery .wrap() method inserts a new parent around each matched element. Pass an HTML string, DOM node, jQuery object, or callback. It is the inverse of .unwrap() — exactly as the official toggle demo demonstrates when a div wrapper is added and stripped on button click.

Choose .wrapInner() to wrap contents inside the element, .wrapAll() to group siblings under one parent, and .unwrap() to remove wrappers. Next up: grouping siblings with .wrapAll().

💡 Best Practices

✅ Do

  • Use .wrap() to add a parent around each matched element
  • Pair with .unwrap() for reversible preview/edit toggles
  • Ensure wrapper HTML has one innermost empty element
  • Use the callback form when wrapper attributes vary per element
  • Use .wrapAll() when siblings should share one container

❌ Don’t

  • Use .wrap() when you only need to wrap inner content
  • Expect text between inline siblings to be included in the wrapper
  • Assume a passed DOM element is moved — it is cloned per match
  • Confuse .wrap() with .wrapInner() or .wrapAll()
  • Provide wrapper HTML with multiple innermost empty elements

Key Takeaways

Knowledge Unlocked

Five things to remember about .wrap()

The add-a-parent API — one wrapper copy per matched element.

5
Core concepts
× 02

Per match

Separate copy

Each
03

.unwrap()

Inverse

Pair
ƒ 04

Callback

Since 1.4

Dynamic
05

vs wrapAll

One vs many

Compare

❓ Frequently Asked Questions

.wrap() inserts a new parent element around each matched node in the jQuery collection. Each match gets its own copy of the wrapper structure. It returns the same jQuery collection. Available since jQuery 1.0 for HTML strings, DOM elements, and jQuery objects; the callback form since 1.4. It is the inverse of .unwrap().
.wrap() places the matched element inside the new wrapper as a direct child of the innermost element. .wrapInner() keeps the matched element as the outer node and wraps its contents instead. Use wrap to add a div around each paragraph; use wrapInner to wrap the text inside a paragraph without adding an outer parent around the p tag itself.
.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.
.unwrap() removes the immediate parent of matched elements and leaves the matched nodes in place. The official jQuery demo toggles wrap and unwrap on button click: .wrap( "<div>" ) adds a div; .unwrap() strips it.
No. When wrapping inline elements like spans, only the matched nodes are wrapped. 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 wrapped.
Yes. Pass an HTML string, selector, DOM element, or jQuery object. jQuery clones the structure for each matched element — 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 .wrap() in version 1.0 as one of the earliest DOM manipulation methods, with the callback form arriving in 1.4 alongside .unwrap(). Before wrap existed, developers manually created elements with document.createElement, called parent.insertBefore( wrapper, child ), and wrapper.appendChild( child ). The span demo in the official API docs exists specifically to show that text between inline siblings is left untouched — a common surprise for beginners expecting entire text runs to be grouped.

Next: jQuery .wrapAll() Method

Wrap all matched elements together in a single shared parent container.

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