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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:13:22+00:00 2026-05-31T00:13:22+00:00

I have multiple divs like this: <div class=small></div> <div class=small></div> <div class=small></div> <div class=small></div>

  • 0

I have multiple divs like this:

<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>

<div class="large"></div>
<div class="large"></div>

<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>

I want to select every 4 .small divs to wrap in another div so it’s like this:

<div class="box">
    <div class="small"></div>
    <div class="small"></div>
    <div class="small"></div>
    <div class="small"></div>
</div>

As you can see from the first example, there can be more than 4 adjacent ones, but I still want to group every 4 .small divs.

Any idea would be much appreciated. Thanks!

  • 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-05-31T00:13:24+00:00Added an answer on May 31, 2026 at 12:13 am

    Here is how I’ve done it in the past.

    var smallDivs = $('div.small'),
        chunkLength = 4;
    
    for (var i = 0, length = smallDivs.length; i < length; i += chunkLength) {
        smallDivs.slice(i, i + chunkLength).wrapAll('<div class="box" />');
    }​
    

    jsFiddle.

    I chunk the selected elements into chunks of a desired length and then call wrapAll() on the sub-selection.

    Just for the hell of it, here is how you’d do it without jQuery…

    var smallDivs = document.querySelectorAll('div.small'),
        chunkLength = 4,
        box;
    
    for (var i = 0, length = smallDivs.length; i < length; i++) {
        if (!(i % chunkLength)) {
            box = document.createElement('div');
            box.className = 'box';
            smallDivs[i].parentNode.appendChild(box);
        }
    
        box.appendChild(smallDivs[i]);
    
    }​
    

    jsFiddle.

    Of course, for old browsers that don’t support document.querySelectorAll() or document.getElementsByClassName(), replace the element selecting code with…

    var divs = document.getElementsByTagName('div'),
        smallDivs = [];
    
    for (var i = 0, length = divs.length; i < length; i++) {
        if ((' ' + divs[i].className + ' ').indexOf(' small ') >= 0) {
            smallDivs.push(divs[i]);
        }
    }
    

    jsFiddle.

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

Sidebar

Related Questions

I have a page with multiples divs like this <div class=bigbox> <a href=http://www.somestuff.com>some stuff</a>
I have a list of DIVs, like this : <div id=list> <div class=classA classB
The HTML looks something like this: <li class=divider> <div> ...(maybe more divs)... <div class=content>
I have multiple divs like setted up below. I want 1 (one) JQuery function
i have multiple divs with the same class on my page. I want to
I have an Html that contains something like: (Multiple divs within div A). <div
I have div having class name .modalPopup inside the div I have multiple table
I have multiple elements each has a short and main divs. I want to
So basically I have a layout like this: <div id=header> <div id=elementAbsolute> <div id=headerElement1>
I have a parent div that is filled with multiple child divs (see here

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.