How to remove only the parent element, without removing the children

2

How to remove only the parent element, without also remove child elements with jQuery. Having this structure for example:

<a href="#">
    <p>A</p>
    <p>B</p>
    <p>C</p>
</a>

With the intention of removing the parent (), after the function the structure would look like this:

<p>A</p>
<p>B</p>
<p>C</p>
    
asked by anonymous 21.12.2016 / 17:46

2 answers

3

Use the unwrap() function in jQuery.

For test purposes I have created a button that simulates.

$('.ativar').on('click', function(){
  if($('p').parent().is('a'))
    $('p').unwrap();
  else
    $('p').wrap('<a href="#"></a>');    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="#">
  <p>A</p>
  <p>B</p>
  <p>C</p>
</a>

<button class="ativar">Ativar/Desativar</button>
    
21.12.2016 / 17:50
2

Another alternative would be to take the content and give a replace:

var children = $("a").contents();
$("a").replaceWith(children);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="#">
    <p>A</p>
    <p>B</p>
    <p>C</p>
</a>
    
21.12.2016 / 17:55