Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:53:01+00:00 2026-06-17T09:53:01+00:00

Looking to use jQuery to wrap an arbitrary amount of user supplied content into

  • 0

Looking to use jQuery to wrap an arbitrary amount of user supplied content into effectively three columns. The initial HTML would looks like this:

<div#content>
    <h2>Page Title</h2>
    <p>Some arbitrary number of paragraphs followed by some arbitrary number of biographies, with an arbitrary number of paragraphs.</p>
    <p>Paragraph.</p>
    <h3>First Person</h3>
    <p>Paragraph.</p>
    <p>Paragraph.</p>
    <h3>Second Person</h3>
    <p>Paragraph.</p>
    <h3>Person Three</h3>
    <p>Paragraph.</p>
    <h3>Person Four</h3>
    <p>Paragraph.</p>
    <h3>Person Five</h3>
    <p>Paragraph.</p>
</div>

The goal is to wrap the content into three columns:

  • The first H2 and subsequent paragraphs would be in column one.
  • The next two H3s and subsequent paragraphs would be in column two.
  • The remaining H3s (in this case three, it could be more) would be in column three.

So…

<div#content>
    <div.col1>
        <h2>Page Title</h2>
        <p>Paragraph.</p>
        <p>Paragraph.</p>
    </div>
    <div.col2>
        <h3>First Person</h3>
        <p>Paragraph.</p>
        <p>Paragraph.</p>
        <h3>Second Person</h3>
        <p>Paragraph.</p>
    </div>
    <div.col3>
        <h3>Person Three</h3>
        <p>Paragraph.</p>
        <h3>Person Four</h3>
        <p>Paragraph.</p>
        <h3>Person Five</h3>
        <p>Paragraph.</p>
        <!-- etc. -->
    </div>
</div>

I have a CodePen set up here: http://codepen.io/ian-pvd/pen/GKryq

It is using the following method:

jQuery('h2').nextUntil('h3').andSelf().wrapAll('<div class="col1" />'); 

jQuery('h3:nth-of-type(1)').nextUntil('h3:nth-of-type(3)').andSelf().wrapAll('<div class="col2" />');

jQuery('h3:nth-of-type(3)').nextUntil('p:last-of-type').wrapAll('<div class="col3" />');

This works right up until the third column, where I’m having trouble wrapping whatever content remains in the third column.

I’ve checked these other questions, which offered some help:

  • Wrap Multiple <p> elements into <div> between <h3>
  • need to wrap h3 and div in wrapper div with jquery
  • jQuery Wrap a div from H2 until last paragraph

But the solutions are a more specific to wrapping from H2 to H2, and not switching between H2 and H3 tags, or including two H3 tags in one set.

Trying to convince the user to enter content differently / separately has already been ruled out for me.

Is there a simple way to do this?

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:53:02+00:00Added an answer on June 17, 2026 at 9:53 am

    The last selector, h3:nth-of-type(3), isn’t matching as expected because you’ve wrapped up the first and second h3 already; the h3 that used to be third is now first-of-type.

    Try wrapping the columns in reverse order instead:

    jQuery('#content>h3:nth-of-type(3)').nextUntil('div').andSelf().wrapAll('<div class="col3" />');
    
    jQuery('#content>h3:nth-of-type(1)').nextUntil('div').andSelf().wrapAll('<div class="col2" />');
    
    jQuery('#content>h2').nextUntil('div').andSelf().wrapAll('<div class="col1" />'); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started looking into the use of jQuery UI. What I would like
I am looking to use jquery to search for a certain string that gets
I'm looking to use jQuery's .load() or .get() functions to replace AJAX, I've got
I'm looking for a complete, step-by-step example of how to use jQuery Mobile 's
I am looking to implement a grid system. I already use jQuery, jQuery UI
Looking to pass variables from c# to javascript to use some jquery code. Passing
I'm looking for the right wildcard character to use with jquery.datepicker.parseDate() in the doc
I am looking for a good jQuery pagination plugin to use in my aspx
I have a group of 4 divs and I'm looking to use jQuery to
I'm looking for a way to have jquery automatically change the content of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.