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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:36:33+00:00 2026-05-13T11:36:33+00:00

I am trying to wrap a certain number of elements in a div .

  • 0

I am trying to wrap a certain number of elements in a div. The problem is that the number of elements can vary based on the user’s input. So the number of elements could be 2, 3, 4, or even more. I have a variable that tells me how many elements should be wrapped. So, for instance, my page may have this:

<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>

Now I need to wrap those in another div based on my variable. So, if my variable held a value of 3, it would look like this:

   <div class="testing"> 
    <div class="test"></div>
    <div class="test"></div>
    <div class="test"></div>   
   </div>

   <div class="testing">
    <div class="test"></div>
    <div class="test"></div>
    <div class="test"></div>
   </div>

I was using this code:

$(this).add($(this).next())
       .add($(this).next().next())
       .wrapAll('<div class="testing"></div>');

The problem with that is that I would need to know how many elements are going to be there. Is there a dynamic way to do this? I also saw the slice function and tried to use it like this:

for(var i=0;i<img_cnt;i+=img_row){
    obj.children().slice(i,i+img_row).wrapAll('<div class="row"></div>');
}

It is not working, though. I have 8 divs. It should be wrapping 3 together, so I should have 3 new divs with 3 in the first 2 and 2 in the last, since there are only 8 divs. However, I get 3 divs in the first new div, then the next 2 divs are not wrapped at all, and then the last 3 divs are wrapped in a new div. I am not sure why it is not wrapping it right. Do you have any ideas on how to do this or maybe even a better method?

  • 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-13T11:36:33+00:00Added an answer on May 13, 2026 at 11:36 am

    Your code isn’t working because children is changing. Try using slice on a constant set:

    var all = $('.test'); 
    for(i=0; i < all.length; i += img_row) {
       all.slice(i, i + img_row).wrapAll('<div class="row" />'); 
    }
    

    Example: http://jsbin.com/upaji

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

Sidebar

Related Questions

Still trying to wrap my head around Clojure. I can see how to implement
I'm trying to wrap all <p> from a div in a sub-div ; the
I am trying to create a custom View that would replace a certain layout
Trying to wrap an tag around an image, addClass and input src of img
Trying to wrap my head around perl's Autovivification and based on what it sounds
Trying to wrap my head around perl's Autovivification and based on what it sounds
I've been trying to wrap my head around solving this issue but I can't
I'm trying to wrap my head around reflection, so I decided to add plugin
I've been trying to wrap my head around how threads work in Python, and
I'm trying to wrap my head around creating a toolbar (a tool band in

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.