jQuery .unwrap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

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

jQuery
$(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)
CallWhat it does
jQuery setElements whose parent will be removed
selectorUnwrap only if parent matches
ReturnSame matched set (chainable)
Since1.4; selector in 3.0

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

Parameters

ParameterDescription
Step 1Select the elements whose parent should go: $("p"), $(".item").
Step 2Call .unwrap() with no args, or pass a selector (jQuery 3.0+) to filter parents.
Step 3Confirm the parent is gone — matched elements (and siblings) stay in the tree.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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

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

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