Title

First paragraph

Second paragraph<...">

How to add a parent to a paragraph group?

I have this HTML:

<div id="description">
  <h5>Title</h5>
  <p>First paragraph</p>
  <p>Second paragraph</p>
  <p>Third paragraph</p>
</div>

I want to insert with jQuery the parent element for all elements <p>except the first. Therefore, the HTML I would like to generate is as follows:

<div id="description">
  <h5>Title</h5>
  <p>First paragraph</p>
  <div class="details">
    <p>Second paragraph</p>
    <p>Third paragraph</p>
  </div>
</div>

There's a function .wrap()in jQuery that can add a parent, but if I use it like this:

$("#description p:not(:first)").wrap('<div class="details" />');

He fully transfers all mine <p>.

Is there any way to change my selector to instead add <div>around the “group”? Or maybe it's easier using another function that is not yet known to me?

Thank!

+5
source share
1 answer

So you are looking for a method .wrapAll().

$("#description p").not(":first").wrapAll('<div class="details" />');

Ref.: . wrapAll()

+13

All Articles