How to Remove Parent Element of any Selected Child Element


You want to remove parent element of any selected element. You can use JQuery unwrap method as shown below.

Let's say you have a div element and a nested span element in your HTML source.

<div id="parent" style="background-color:yellow; padding:5px;"> 

   <span id="child" style="background-color:green;">I am alone, because my parent DIV has been removed!</span> 

</div>

You can remove the parent div element of child span as follows:

$("#child").unwrap();

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments 2

user
Novi filmovi
  • 10 January 2013

This is not doing anything at all. At least for me. What about $(this).parents("div").remove(); ?

user
Kuldeep Kamlesh Kaushik
  • 06 August 2013

Thanks very much for your valuable guidance !!
You save me from lots of hard work as I was struggling with a automatic generated html in Express Engine CMS.

Add Comment