-3

I'm trying to remove the following DIV's:

<div class="whatever_name">
   <div class="whatever_name">
      <h2>subtitle</h2> 
      <p>content<p>
   </div>
</div>

and need the following output:

      <h2>subtitle</h2> 
      <p>content<p>

Using jQuery, I can not use remove() because it clear the content too. With pure javascript, happens the same.

I have no idea how to accomplish this issue.

Any idea?

EDIT:

Not always the structure is the same. It can vary, i.e.:

<div class="whatever_name">
   <div class="whatever_name">
      <div class="whatever_name">
         <h2>subtitle</h2> 
         <p>content<p>
      </div>
   </div>
</div>

Just need an iterator that can handle such task.

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
domoindal
  • 1,513
  • 2
  • 17
  • 33

1 Answers1

1

Use unwrap() method twice on the children element.

$('.content .post-12')
  //  get children elements, or use contents()
  .children()
  // use unwrap twice to unwrap two parents
  .unwrap().unwrap()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
  <div class="post-12">
    <h2>subtitle</h2>
    <p>content</p>
  </div>
</div>

UPDATE : With the updated content you just need to change the selector with your requirement.

$('div > div > h2,div > div > p').unwrap().unwrap()

// or use 
// $('div > div:has(h2):has(p) > *')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="whatever_name">
  <div class="whatever_name">
    <div class="whatever_name">
      <h2>subtitle</h2>
      <p>content
        <p>
    </div>
  </div>
</div>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • 1
    Hi, Although firstly i do not know class names, your solution is valid because I can iterate over divs. I did not know the unwrap function. Thanks! – domoindal Dec 01 '17 at 17:45