Exploring the unwrap() method in jQuery 1.4

As given in the jQuery API documentation, the .unwrap() method removes the element's parent. The matched elements (and their siblings, if any) replace their parents within the DOM structure.

Here’s an example:

<html>
<
head>
<
title>Exploring the unwrap method</title>
<
style type="text/css">
.divOne{
height:40px;
width:100px;
background-color:#808080;
}
.divTwo{
height:20px;
width:50px;
background-color:#f0f0f0;
}
</style>
<
script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
</
script>
<
script language="javascript" type="text/javascript">
$(function() {
$("#btn").click(function() {
jQuery('.divTwo').unwrap();
});
});
</script>
</
head>
<
body>
<
form>
<
div class="divOne">
<
div class="divTwo">

</
div>
</
div>
<
input id="btn" type="button" value="Unwrap" />
</
form>
</
body>
</
html>

If you run the code, the display is similar to the following:

image

Clicking on Unwrap, calls the unwrap() method on the jQuery object representing divTwo. The output is as follows:

image

As shown above, unwrap removes the parent of divTwo.

See a Live Demo



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: