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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:01:00+00:00 2026-05-17T15:01:00+00:00

Using jQuery, I’m trying to group similar items in a list. Here’s what I’m

  • 0

Using jQuery, I’m trying to group similar items in a list. Here’s what I’m trying to do. Given a list like the following:

<ul>
    <li class="foo">Item #1</li>
    <li class="foo">Item #2</li>
    <li class="foo">Item #3</li>
    <li class="bar">Item #4</li>
    <li class="bar">Item #5</li>
    <li class="foo">Item #6</li>
    <li class="foo">Item #7</li>
    <li class="bar">Item #8</li>
</ul>

I’d like to end up with the following:

<ul>
    <li class="foo">Item #1 <a>2 More Like This</a>
        <ul>
            <li class="foo">Item #2</li>
            <li class="foo">Item #3</li>
        </ul>
    </li>
    <li class="bar">Item #4</li>
    <li class="bar">Item #5</li>
    <li class="foo">Item #6 <a>1 More Like This</a>
        <ul>
            <li class="foo">Item #7</li>
        </ul>
    </li>
    <li class="bar">Item #8</li>
</ul>

In short, anytime there’s 2 or more items with class=”foo”, they should be grouped together up until reaching a non-class=”foo” item. I can then use a link to show or hide the grouped items.

  • 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-17T15:01:00+00:00Added an answer on May 17, 2026 at 3:01 pm

    Here’s one possibility:

    Example: http://jsbin.com/ipiki3/3/

    $('ul > li.foo')
        .filter(function() {
            var $th = $(this);
            return $th.next('li.foo').length && !$th.prev('li.foo').length;
        })
        .each(function() {
            var $th = $(this);
            var len= $th.append('<a href="#"> more like this</a>')
                        .nextUntil(':not(li.foo)').wrapAll('<ul>').length;
            $th.next('ul').appendTo(this);
            $th.children('a').prepend(len);
        });
    

    EDIT: Fixed a mistake with the len variable, and added an example.


    Explanation: What’s happening with the .filter() is that it is narrowing the li.foo elements down to the first in a group (has at least one li.foo after it, and none before).

    Then with the .each() it appends the <a>, get’s the next elements until it reaches one that is not li.foo, wraps those with a <ul> and returns how many there were using the length property.

    Then we traverse over to that new <ul> and append it to the first li.foo in the group.

    Finally we prepend the quantity we stored in the length property to the <a> element.

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

Sidebar

Related Questions

Using jQuery I'd like to add additional top-margin to a div 'foo', but only
Using jQuery I'm programmatically generating a bunch of div 's like this: <div class=mydivclass
Using jQuery Nearest Element , I would like to select elements in a circular
Using jQuery, how can I get the selected item's value in a select when
Using JQuery I am trying to perform validation and also get the values of
Using jQuery, I'm trying to change the selected option in a SELECT element to
Using jQuery and given this <ul> <li> <a external=http://stackoverflow.com href=home.htm>Link 1</a> </li> <li> <a
Using jQuery 1.7.1, I'm trying to create an event handler for the change event
Using jQuery and backbone.js, I would like to separate the JavaScript from the HTML
(using jquery) I have the following function $(function() { $('tr.parent') .css(cursor,pointer) .attr(title,Click to expand/collapse)

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.