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

  • Home
  • SEARCH
  • 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 8072673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:05:37+00:00 2026-06-05T14:05:37+00:00

I am having a hard time figuring this out, but I am sure it

  • 0

I am having a hard time figuring this out, but I am sure it is simple.

I know that I have to use :nth-child to count every set of elements I want to wrap. I am wondering how I would count to :nth-child and wrap all the previous elements including the :nth-child in a div for each set of elements that match :nth-child. I am assuming there is an .each() involved.

The code would be laid out like so:

<div class="wrapper"> 
<h3>Heading</h3>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>

<h3>Heading</h3>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>


<h3>Heading</h3>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>


<h3>Heading</h3>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>

Update Tried this piece of code and it seems to give me the desired results, but stops at 16.

  $(".wrapper").each( function () {
$(this).children(":lt(16)").wrapAll("<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-05T14:05:40+00:00Added an answer on June 5, 2026 at 2:05 pm

    jQuery’s .each() function has a built in index – so we can use that to our advantage to select every 16 elements.

    $("div.wrapper").children().each(function(i){
        if(i % 16 == 0 && i != 0){
            // We get the elements we need to wrap, but they're going to be in reverse order (thanks to .prevAll())
            var elementsToWrap = $(this).prevAll().not('.wrap2');
            var temp = [];
            for(var j=0; j < elementsToWrap.size(); j++){
                // Reverse the objects selected by placing them in a temporary array.
                temp[(elementsToWrap.size()-1) - j] = elementsToWrap.eq(j)[0];
            }
            // Wrap the array back into a jQuery object, then use .wrapAll() to add a div around them
            $(temp).wrapAll('<div class="wrap2" />');
        }
    });
    

    ​
    DEMO http://jsfiddle.net/CRz7E/1/

    If you wanted to wrap each element separately – instead of as a group, then you don’t need to reverse the selection, and you could just use .wrap()

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

Sidebar

Related Questions

Having a hard time figuring out the best way to do this... I have
This is probably very easy, but I'm having a hard time figuring it out.
I'm having a hard time figuring out how best to do this. I have
I'm having a hard time figuring this out but how do I tell my
Ok, I have been having a VERY hard time figuring this out. I guess
Having a hard time figuring this one out. I have 3 tabs at the
I'm having a hard time figuring this one out. Seems like no matter what
This should be an easy question - but I'm having a hard time figuring
Having a hard time figuring out the best way to go about this. What
Apologies if this is a repeat; I'm having a hard time figuring out what

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.