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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:44:14+00:00 2026-06-15T22:44:14+00:00

Duplicate: How can I add a parent element to a group of paragraph? I

  • 0

Duplicate:
How can I add a parent element to a group of paragraph?

I have the following HTML blocks repeated in the document

<!-- first block -->
<div class="first">
   My first div
</div>
<div class="second">
   My second div
</div>

<!-- second block -->
<div class="first">
   My first div
</div>
<div class="second">
   My second div
</div>

...

How can I wrap the Divs with jQuery to get the resulting HTML like this…

<!-- first block -->
<div class="container">
   <div class="first">
      My first div
   </div>    
   <div class="second">
      My second div
   </div>
</div>

<!-- second block -->
<div class="container">
   <div class="first">
      My first div
   </div>    
   <div class="second">
      My second div
   </div>
</div>

...
  • 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-15T22:44:15+00:00Added an answer on June 15, 2026 at 10:44 pm

    You’re in luck, that’s exactly what wrapAll is for:

    $(".first, .second").wrapAll('<div class="container"></div>');
    

    Live Example | Source


    Your edit markedly changes the question. If you need to do the above only within some containing block, you can loop through the containing blocks and apply wrapAll only to their contents. You’ll need a way to identify the way you want to group your divs, which you haven’t specified in the question.

    If the divs have some kind of container around them, you can do this:

    $(".block").each(function() {
      $(this).find(".first, .second").wrapAll('<div class="container"></div>');
    });
    

    In that example, I’ve assumed the divs are within a container with the class "block".

    Live Example | Source

    If there’s no structural way to identify them, you’ll have to do it some other way. For instance, here we do it by assuming any time we see a first, we should stop grouping:

    var current = $();
    
    $(".first, .second").each(function() {
      var $this = $(this);
      if ($this.hasClass('first')) {
        doTheWrap(current);
        current = $();
      }
      current = current.add(this);
    });
    doTheWrap(current);
    
    function doTheWrap(d) {
      d.wrapAll('<div class="container"></div>');
    }
    

    Live Example | Source

    That works because $() gives you the elements in document order, so if we loop through them in order, saving them up, and then wrap up the previous ones whenever we see a new first (and of course, clean up at the end), you get the desired result.

    Or here’s another way to do that same thing, which doesn’t use wrapAll. It relies on the first matched element being a first (so no seconds before firsts!):

    var current;
    
    $(".first, .second").each(function() {
      var $this = $(this);
      if ($this.hasClass('first')) {
        current = $('<div class="container"></div>').insertBefore(this);
      }
      current.append(this);
    });
    

    Live Example | Source

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

Sidebar

Related Questions

Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following
Possible Duplicate: How can I add numbers in a bash script I have a
Possible Duplicate: How can I add resizable widgets in Qt Creator? I am using
Possible Duplicate: Can I scroll a ScrollView programmatically in Android? I have a chat
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: Can I just make up attributes on my HTML tags? Hi, I
Possible Duplicate: can’t add data to jqGrid from php within json format I am
Possible Duplicate: How can I add a Trace() to every method call in C#?
Possible Duplicate: Can I add custom methods/attributes to built-in Python types? In Ruby you
Possible Duplicate: Can I add buttons to jQuery “alert”? When I click OK or

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.