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

The Archive Base Latest Questions

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

I have groups of unordered lists and I am trying to divide each list

  • 0

I have groups of unordered lists and I am trying to divide each list group into sub-groups of 3 dynamically. So far I have this:

var uls = $("ul > li");
for(var i = 0; i < uls.length; i+=3) {
uls.slice(i, i+3).wrapAll("<ul class='new'></ul>");
}

… for some sample HTML below

<h2>Group 1</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>

<h2>Group 2</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
</ul>

The issue I am having is that my code just lumps all the <ul>s together without regard to groups, i.e. ‘group 1’ ‘group 2’ I tried using .each(function() { on $("ul > li") but I just get errors. I also tried moving down the each function after .length but that did not work either.

So the final HTML would look like this:

<h2>Group 1</h2>
<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
</ul>

<ul>
    <li>Item 4</li>
    <li>Item 5</li>
</ul>

<h2>Group 2</h2>
    <ul>
etc... 

My fiddle is here but as you can see it’s not quite working yet.

  • 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-14T22:11:04+00:00Added an answer on June 14, 2026 at 10:11 pm

    How’s this? http://jsfiddle.net/4Bms6/

    $('ul').each(function(){
        var uls = $("li", this);
    
        for(var i = 0; i < uls.length; i+=3) {
    
        var lis = $("li", this);
    
            uls.slice(i, i+3).wrapAll("<ul class='new'></ul>");
        }
    });​
    

    The reason you were getting incorrect grouping is because you were starting off by collecting every single li (“ul > li” equates to all li that are children of any ul). What you should have been doing is selecting all li that are children of each ul, perform the grouping, then move on to the next ul.

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

Sidebar

Related Questions

I have three unordered lists each containing a subset of a group of items
I have got an simple html unordered list. <ul> <li>Item 1</li> <li> Group 1
so i have a simple unordered list: <ul id=nav> <li> <span>Group 1 »</span> <ul>
I have several groups of elements, each group of which is related to a
we have 6 groups of 253 machines each one , but each group are
I am trying to make a navigation that uses simple unordered lists to list
I have 2 groups of radio buttons with links corresponding to each of the
I'm trying to create an unordered list (UL) in my div #dropZone if one
I have a list of 179 thumbnail images that I am trying to apply
I have groups of students that need to be allocated into classrooms of a

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.