views:

483

answers:

4

There is a great method in jquery called wrap() that will wrap a selected element inside a new element, like so:

Start with:

 <p>I wish I was wrapped!</p>

Add code:

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

End with:

<div><p>I wish I was wrapped!</p></div>

But what I need is something that will unwrap, so that the above process is reversed. It seems that the issue is that when you select a bad item (let's say an unnecessary table) that it always grabs what is inside it as well, so if I want to remove all <td>s, I am left with nothing, since that removed the td and anything inside.

Is there a standard reliable way of removing elements but leaving any children/ancestors alone?

+1  A: 

A quick Google search reveals that there is such functionality, in the form of a small 576 byte plugin called jqueryunwrap. I have not tried it personally, but it is worth a shot. ;)

Furutsuzeru
I figured there would be plugins. I was hoping for something a bit more reliable/standardized. Good find though.
Anthony
A: 

Check out this SO answer if you want more details

fudgey
+1  A: 

In JQuery 1.4 unwrap() was added: http://api.jquery.com/unwrap/

lepe
Thanks for the heads up! I'm checking out all of the other features that slipped under my radar in 1.4.
Anthony
A: 

$("p").unwrap() will unwrap the wrapping div....................I hope this helps

Prabeen giri