Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Get all elements between element type (h1's) and wrap in div

I have the below structure:

<div class="wrapper">
    <h1>Header 1</h1>
    <h2>subtitle 1</h2>
    <p>aaa</p>
    <p>bbb</p>
    <p>ccc</p>
    <h2>subtitle2</h2>
    <p>ddd</p>

    <h1>Header 2</h1>
    <h2>subtitle 3</h2>
    <p>eee</p>
    <p>fff</p>

</div>

I want to select all the h1 and p elements between each h1 and wrap in a div, so I end up with:

<div class="wrapper">
    <div>
        <h1>Header 1</h1>
        <h2>subtitle 1</h2>
        <p>aaa</p>
        <p>bbb</p>
        <p>ccc</p>
        <h2>subtitle2</h2>
        <p>ddd</p>
    </div>

    <div>
        <h1>Header 2</h1>
        <h2>subtitle 3</h2>
        <p>eee</p>
        <p>fff</p>
    </div>
</div>

I've tried various things like the below, but none work perfectly:

$( "h1" ).prevUntil( "h1" ).wrapAll( "<div></div>" );
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  1. You shall use .each jQuery API to accomplish.
$("h1").each(function(){})
  1. .prevUntil will inverse the Dom node traversing, use .nextUntil instead.

  2. as to match the end-up output, the script is as follows:

<script>
    $("h1").each(function() {
        $(this).nextUntil( "h1" ).wrapAll( "<div></div>" );
        // uncomment the two lines to move the h1 inside the wrapped div
        // const el = $(this).next();
        // $(this).prependTo(el);
    });
</script>
about 4 years ago · Juan Pablo Isaza Report

0

I came up with this solution where it gets all the children and then if its a h1 tag it will create a new div and move H1 and other children into it

    var newDiv;
    $(document).ready(function(){
        $(".wrapper").children().each(function(){
           if($(this).is("h1")){
              $(this).before("<div></div>");
              newDiv =  $(this).prev();
              newDiv.append($(this));
           }else{
            newDiv.append($(this));
           }
        });
       
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">

        <h1>Header 1</h1>
        <h2>subtitle 1</h2>
        <p>aaa</p>
        <p>bbb</p>
        <p>ccc</p>
        <h2>subtitle2</h2>
        <p>ddd</p>

        <h1>Header 2</h1>
        <h2>subtitle 3</h2>
        <p>eee</p>
        <p>fff</p>
 
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!