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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:08+00:00 2026-05-23T17:40:08+00:00

Hi I’ve got this list of li’s, containing dynamic classes(not always the same). There’s

  • 0

Hi I’ve got this list of li’s, containing dynamic classes(not always the same). There’s always a .connected class and two sets of "numberclasses".

<ul>
  <li class="connected 34 22"></li>
  <li class="connected 54 11"></li>
  <li class="connected 11 54"></li>
  <li class="connected 22 34"></li>
  <li class="connected 12 31"></li>
  <li class="connected 31 12"></li>  
</ul>

I want to wrap/sort the classes in groups like this:

<ul>
  <li>
    <ul>
      <!-- Group consisting elements with classes 12 or 31 -->
      <li class="connected 12 31">foo</li>
      <li class="connected 31 12">foo</li>
    </ul>
  </li>
  <li>
    <ul>
      <!-- Group consisting elements with classes 22 or 34 -->
      <li class="connected 34 22">foo</li>
      <li class="connected 22 34">foo</li>
    </ul>
  </li>
    ...
</ul>

How can i do this?

— Update —

My sorting logic is that I want to wrap elements that contains the same classes.

I’ve tried something like $('.22').wrapAll('<ul/>'); but since the classes are generated and not static i can’t find a way to do it.

— Update 2 —

Think i know how I can do this. But I’m gonna need a little help.

First i push all class values from the list items to an array.
Then remove all duplicates in that array.
Later on iterate the array and wrap these array values in ul’s. $('array[1]').wrapAll('<ul/>');

jsfiddle link

  • 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-23T17:40:09+00:00Added an answer on May 23, 2026 at 5:40 pm

    demo: http://jsfiddle.net/JX58j/

    jquery

    $(document).ready(function() {
    
        var classMap = [];
    
        $('.connected').each(function(idx) {
            var $this = $(this);
    
            // get the classes of each LI and sort them (It's easier to compare this way)
            var sortedClasses = $this.attr('class').split(' ').sort().toString().replace(/,/g, '-');
    
            // store the resulting sorted classes as data
            $this.data('grouping', sortedClasses);
    
    
            // add the 'grouping' to the classMap array if it isn't already in there
            if ($.inArray(sortedClasses, classMap) == -1) {
                classMap.push(sortedClasses);
            }
        });
    
        // create the ULs determined by the length of classMap
        for (i = 0; i < classMap.length; i++) {
            $('<ul>')
                .appendTo('#old-ul')
                .addClass(classMap[i]);           
        }
    
        // move the LIs from old UL to the new ULs
        $('.connected').each(function() {
            $this = $(this);
            $grouping = $this.data('grouping');
            $this.appendTo('ul.' + $grouping);
        });
    
        // wrap the ULs inside #old-ul in an LI tag so it validates
        $('#old-ul ul').wrap('<li />');
    
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string

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.