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 8328371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:25:13+00:00 2026-06-09T01:25:13+00:00

I need to wrap some divs into another div with jQuery, the original output

  • 0

I need to wrap some divs into another div with jQuery, the original output looks like this:

<div>Some content</div>
<div>Some content</div>

<h3>Local</h3>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>

<h3>Non-Local</h3>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>

And I would need to wrap them so they look like this:

<div>
    <div>Some content</div>
    <div>Some content</div>
</div>

<div>
    <h3>Local</h3>
    <div>Some content</div>
    <div>Some content</div>
    <div>Some content</div>
</div>

<div>
    <h3>Local</h3>
    <div>Some content</div>
    <div>Some content</div>
    <div>Some content</div>
</div>

The divs can’t be identified by any id or class, so I would need to find a way to wrap all divs that follow an h3.

  • 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-09T01:25:15+00:00Added an answer on June 9, 2026 at 1:25 am

    You could

    • loop over the h3 elements
    • use the nextUntil() – function of jQuery to look for div elements following the h3,
    • add them to the collection
    • push each of them into the group array
    • and then loop over the groups, and wrap each of them inside a div

      var groups = [];

      $(‘h3’).each(function(index, value) {

      $head = $(value);

      var $group = $head.add($head.nextUntil(‘h3’, ‘div’));
      groups.push($group);

      });

      for (group in groups) {

      $(groups[group]).wrapAll(”);

      }

    The reason to save the groups in a groups array first is, to not change the HTML structure until having looped over each collection. Otherwise, the subsequent calls will find the added elements as well.

    Here’s the example code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create this layout in Silverlight. The text need to wrap
I have some div's like <div class=block-wrap> <a href=#>A</a> <a href=#>A</a> <a href=#>A</a> <a
I need to wrap up an array of elements into a jQuery object as
Most of the time when I need to wrap some code onto the next
I have an XSLT file generating plain HTML. I need to wrap some elements
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to wrap a List<T> with some class that allows calls to set/get
I need to wrap zend form error messages in custom html. <div class=cerror id=ID-error>
I'm working on a .Net 2.0 application and need to wrap some database transactions
I have a naming problem for some of my classes. I need to wrap

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.