jQuery .unwrap() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .unwrap()?

.unwrap() removes the parent of each matched element and leaves those elements in the DOM where the parent was (since jQuery 1.4). It is the inverse of .wrap(). Returns the same jQuery set for chaining.

Remember
$(els).unwrap()              // remove immediate parent
$(els).unwrap(".wrapper")    // jQuery 3.0+ — only if parent matches

Before:  <div><p>Hi</p></div>
After:   <p>Hi</p>

siblings move up with the matched elements
≠ .remove()  (remove deletes the matched els)
NeedUse
Strip one parent layer$(els).unwrap()
Unwrap only matching parents$(els).unwrap(".box") (3.0+)
Add a parent around each$(els).wrap("<div>")
Delete the matched elements$(els).remove()

How to Use .unwrap()

Select the inner elements, then unwrap their parents.

  1. Select the elements whose parent should go: $("p"), $(".item").
  2. Call .unwrap() with no args, or pass a selector (jQuery 3.0+) to filter parents.
  3. Confirm the parent is gone — matched elements (and siblings) stay in the tree.
  4. Chain further methods on the same selection if needed.
PieceRequired?Role
jQuery setYesElements whose parent will be removed
selectorNo (3.0+)Unwrap only if parent matches
Return—Same matched set (chainable)
Since—1.4; selector in 3.0

Pseudocode

Pseudocode
unwrap(optionalSelector):
    for each element in this set:
        parent = element.parent
        if optionalSelector and parent does not match:
            skip
        else:
            move parent's children into parent's place
            remove parent from the DOM
    return this set

Cheat Sheet

Keep these facts handy when stripping wrappers.

TopicDetail
Syntax.unwrap([selector])
RemovesImmediate parent (not the matched els)
KeepsMatched elements + their siblings
Inverse.wrap()
SelectorjQuery 3.0+ only
ReturnsOriginal matched set
SincejQuery 1.4

Live Preview

Run unwrap on a small HTML sketch and watch the parent disappear.

Browser demo of .unwrap() — same ideas as the examples below.

Result
before → <div><p>Hi</p></div>
after  → <p>Hi</p>

/* parent div removed */

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Basic .unwrap()

Remove the parent div and keep the paragraph.

jQuery
$("#msg").unwrap();

console.log($("#host").html());
// <p id="msg">Hello</p>
Try It Yourself

How It Works

  1. Start. HTML is <div><p id="msg">Hello</p></div>.
  2. Select. $("#msg") targets the paragraph.
  3. Unwrap. The parent div is removed.
  4. Result. The paragraph stays under #host.

Example 2 — Wrap / Unwrap Toggle

Official idea: wrap each paragraph, then unwrap on the next click.

jQuery
var $p = $("p.demo");

if ($p.parent().is("div") && !$p.parent().is("#host")) {
  $p.unwrap();
} else {
  $p.wrap("<div>");
}

console.log($("#host").html());
Try It Yourself

How It Works

  1. Check parent. Skip the page host; unwrap only real wrappers.
  2. Wrapped? Call .unwrap() to strip each parent div.
  3. Not wrapped? .wrap("<div>") wraps each paragraph.
  4. Toggle. Each run flips between wrapped and unwrapped.

Example 3 — Selector Filter (jQuery 3.0+)

Unwrap only when the parent matches a selector.

jQuery
$("#a").unwrap(".box");   // parent matches → unwrapped
$("#b").unwrap(".box");   // parent is <section> → skipped

console.log($("#host").html());
Try It Yourself

How It Works

  1. Match. #a sits in div.box — selector matches → unwrap.
  2. Miss. #b sits in section — selector fails → stay wrapped.
  3. Safe. Use a selector when only some wrappers should be removed.
  4. Version. Needs jQuery 3.0+.

Example 4 — Siblings Stay Together

Unwrapping promotes every child of the parent, not only the matched one.

jQuery
$("#keep").unwrap();

console.log($("#host").html());
// <p id="keep">Keep</p><span>Sibling</span>
Try It Yourself

How It Works

  1. Start. A div holds #keep and a sibling span.
  2. Unwrap one. Only #keep is selected.
  3. Promote all. Both children replace the parent.
  4. Rule. Siblings are not discarded when the wrapper goes.

Example 5 — .unwrap() vs .remove()

Unwrap keeps the element; remove deletes it.

jQuery
$("#u").unwrap();          // keeps #u, removes its parent
$("#r").remove();          // deletes #r

console.log($("#host").html());
// <p id="u">Unwrap me</p>
Try It Yourself

How It Works

  1. unwrap. Parent of #u is removed; #u stays.
  2. remove. #r (and its wrapper content) is deleted from the DOM.
  3. Compare. Strip a wrapper → unwrap; delete the node → remove.
  4. Pick by intent. Keep children vs delete the selection.

Edge Cases & Pitfalls

Check these before stripping wrappers in production.

parent

Only the immediate parent

.unwrap() removes one layer. Nested wrappers need multiple calls (or a loop).

body

Do not unwrap past useful roots

Unwrapping when the parent is body / document root is usually wrong — keep a sensible container.

selector

Selector is jQuery 3.0+

On 1.x/2.x, pass no argument. Check the parent yourself with .parent().is(...) if needed.

siblings

Siblings come along

All children of the removed parent are promoted — not only the matched element.

Browser Support

.unwrap() needs jQuery 1.4+. The optional selector argument needs jQuery 3.0+.

✓ jQuery 1.4+

.unwrap()

Works in Chrome, Firefox, Safari, and Edge when jQuery 1.4+ is loaded. Use 3.0+ for .unwrap(selector).

100% With jQuery 1.4+
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
.unwrap() Universal

Bottom line: Safe for modern jQuery. Prefer a selector (3.0+) when only specific parent wrappers should be removed.

Performance Notes

ApproachCostBest for
.unwrap()DOM move + remove parentStrip one wrapper layer
.unwrap(sel)Same + parent match testSelective unwrap (3.0+)
Repeated unwrap in a loopCost × layersDeep nested wrappers

Cost scales with how many parents you remove. Prefer one clear unwrap over many accidental calls in hot paths.

Key Takeaways

  • Role: remove the parent; keep the matched elements.
  • Inverse: pair with .wrap() to add / strip wrappers.
  • Selector: jQuery 3.0+ filters which parents to remove.
  • ≠ remove: unwrap keeps children; remove deletes the selection.

One line: .unwrap() strips the parent wrapper and leaves the matched elements in place.

Frequently Asked Questions

.unwrap() removes the immediate parent of each matched element and leaves those elements (and their siblings) in place. It returns the same jQuery set for chaining. Available since jQuery 1.4; optional selector since 3.0.
.unwrap() deletes the parent wrapper and keeps the matched elements. .remove() deletes the matched elements themselves.
Yes. .wrap() adds a parent around matched elements; .unwrap() removes that parent and promotes the children.
Since jQuery 3.0, yes. .unwrap(".box") removes the parent only if it matches the selector. If the parent does not match, that element is not unwrapped. With no argument, the immediate parent is always removed.
All children of the removed parent — matched elements and siblings — are promoted together to replace the parent in the DOM.
The original jQuery collection of matched elements (not the removed parents), so you can keep chaining.

Next: .wrap()

Learn how to add a parent wrapper around each matched element.

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

9 people found this page helpful