Reputation: 77
I've been digging around for a while now and can't find a solution. I would be very grateful if someone could help me on this one.
basically I have a list like so:
<ul class="list-items" id="1">
<li class="3"><a href="/courses/art-design-and-photography/hnc-3d-computer-animation">3D Computer Animation</a> HNC SCQF Level 7 </li>
<li class="A"><a href="/courses/business-and-management/hnc-accounting">Accounting</a> HNC SCQF Level 7 </li>
<li class="A"><a href="/courses/business-and-management/hnd-accounting">Accounting</a> HND SCQF Level 8 </li>
<li class="A"><a href="/courses/business-and-management/ncga-administration">Administration</a> NCGA SCQF Level 5 </li>
<li class="A"><a href="/courses/business-and-management/svq-administration">Administration</a> SVQ SCQF Level 5 </li>
<li class="A"><a href="/courses/business-and-management/hnc-administration-information">Administration & Information</a> HNC SCQF Level 7 </li>
<li class="A"><a href="/courses/business-and-management/hnd-administration-information">Administration & Information</a> HND SCQF Level 8 </li>
<li class="A"><a href="/courses/computing-and-it/advanced-ecdl-word-processing-2013">Advanced ECDL: Word Processing 2013 </a> Equivalent to SCQF Level 4 </li>
<li class="A"><a href="/courses/business-and-management/hnd-advertising-pr">Advertising & PR</a> HND SCQF Level 8 </li>
<li class="A"><a href="/courses/care/award-alcohol-awareness">Alcohol Awareness</a> Award, Level 1 Equivalent to SCQF Level 4 </li>
<li class="A"><a href="/courses/animal-care/introduction-animal-care">Animal Care</a> Introduction SCQF Level 4/5 </li>
<li class="A"><a href="/courses/animal-care/nc-animal-care">Animal Care</a> NC SCQF Level 5 </li>
<li class="A"><a href="/courses/animal-care/hnd-animal-care">Animal Care</a> HND SCQF Level 8 </li>
<li class="A"><a href="/courses/art-design-and-photography/art-design">Art & Design</a> SCQF Level 4/5 </li>
<li class="A"><a href="/courses/art-design-and-photography/nc-art-design">Art & Design</a> NC SCQF Level 6 </li>
<li class="A"><a href="/courses/art-design-and-photography/hnc-art-design">Art & Design</a> HNC SCQF Level 7 </li>
<li class="A"><a href="/courses/art-design-and-photography/hnd-art-design">Art & Design</a> HND SCQF Level 8 </li>
<li class="A"><a href="/courses/automotive-and-construction/diploma-automotive-accident-repair-principles-body-paint">Automotive Accident Repair Principles (Body & Paint)</a> Diploma SCQF Level 4 </li>
<li class="A"><a href="/courses/automotive-and-construction/diploma-automotive-maintenance-repair-principles">Automotive Maintenance & Repair Principles</a> Diploma, Level 1 SCQF Level 4 </li>
<li class="A"><a href="/courses/automotive-and-construction/diploma-automotive-maintenance-repair-principles-0">Automotive Maintenance & Repair Principles</a> Diploma, Level 2 SCQF Level 5 </li>
<li class="B"><a href="/courses/automotive-and-construction/nc-built-environment">Built Environment</a> NC SCQF Level 5/6 </li>
<li class="B"><a href="/courses/automotive-and-construction/hnc-built-environment">Built Environment</a> HNC SCQF Level 7 </li>
<li class="B"><a href="/courses/business-and-management/ncga-business">Business</a> NCGA SCQF Level 5 </li>
<li class="B"><a href="/courses/business-and-management/hnc-business">Business</a> HNC SCQF Level 7 </li>
<li class="B"><a href="/courses/business-and-management/hnd-business">Business</a> HND SCQF Level 8 </li>
<li class="B"><a href="/courses/business-and-management/introduction-business-administration">Business & Administration</a> Introduction SCQF Level 4 </li>
<li class="B"><a href="/courses/business-and-management/ncga-business-with-esol">Business with ESOL</a> NCGA SCQF Level 4 </li>
<li class="C"><a href="/courses/art-design-and-photography/nc-computer-arts-animation">Computer Arts & Animation</a> NC SCQF Level 6 </li>
<li class="C"><a href="/courses/automotive-and-construction/introduction-construction-crafts">Construction (Crafts)</a> Introduction SCQF Level 4/5 </li>
<li class="E"><a href="/courses/esol/pre-intermediate-english-for-speakers-of-other-languages-esol">English for Speakers of Other Languages (ESOL)</a> Pre-intermediate, Pre-intermediate Equivalent to SCQF Level 5 </li>
<li class="E"><a href="/courses/esol/pre-intermediate-english-for-speakers-of-other-languages-pt">English for Speakers of Other Languages (ESOL)</a> Pre-intermediate, Pre-intermediate SCQF Level 1 </li>
<li class="E"><a href="/courses/business-and-management/hnd-events-management">Events Management</a> HND SCQF Level 8 </li>
<li class="F"><a href="/courses/art-design-and-photography/nc-fashion-design-manufacture">Fashion Design & Manufacture</a> NC SCQF Level 6 </li>
<li class="F"><a href="/courses/art-design-and-photography/city-guilds-level-2-diploma-floristry">Floristry</a> City & Guilds Level 2 Diploma SCQF Level 5 </li>
<li class="G"><a href="/courses/automotive-and-construction/npa-general-building">General Building</a> NPA SCQF Level 5 </li>
<li class="I"><a href="/courses/computing-and-it/it-in-business-word-processing-and-presentations">IT in Business: Word Processing and Presentations</a> Equivalent to SCQF Level 5 </li>
<li class="L"><a href="/courses/business-and-management/hnd-legal-services">Legal Services</a> HND SCQF Level 8 </li>
<li class="P"><a href="/courses/art-design-and-photography/nc-photography">Photography</a> NC SCQF Level 6 </li>
<li class="P"><a href="/courses/art-design-and-photography/hnd-photography">Photography</a> HND SCQF Level 8 </li>
<li class="R"><a href="/courses/engineering/rya-day-skipper-shore-based-course">RYA Day Skipper Shore Based Course</a> </li>
<li class="S"><a href="/courses/care/supporting-people-with-down-syndrome">Supporting People with Down Syndrome</a> Equivalent to SCQF Level 4 </li>
<li class="T"><a href="/courses/art-design-and-photography/hnd-textiles">Textiles</a> HND SCQF Level 8 </li>
<li class="T"><a href="/courses/engineering/thermofluids">Thermofluids</a> Equivalent to SCQF Level 4 </li>
<li class="T"><a href="/courses/business-and-management/nc-travel-tourism">Travel & Tourism</a> NC SCQF Level 5 </li>
<li class="T"><a href="/courses/business-and-management/hnd-travel-tourism">Travel & Tourism</a> HND SCQF Level 8 </li>
<li class="T"><a href="/courses/business-and-management/nc-travel-tourism-with-esol">Travel & Tourism with ESOL</a> NC SCQF Level 5 </li>
<li class="U"><a href="/courses/care/certificate-understanding-end-of-life-care">Understanding End of Life Care</a> Certificate, Level 2 Equivalent toSCQF Level 4 </li>
<li class="V"><a href="/courses/animal-care/city-guilds-level-3-diploma-veterinary-nursing-small-animal">Veterinary Nursing (Small Animal)</a> City & Guilds Level 3 Diploma SCQF Level 6 </li>
<li class="V"><a href="/courses/art-design-and-photography/hnd-visual-communication">Visual Communication</a> HND SCQF Level 8 </li>
<li class="W"><a href="/courses/computing-and-it/advanced-word">Word</a> Advanced Equivalent to SCQF Level 4 </li>
</ul>
I need to get all these list items and group them into arrays by letter/number, I can then count them and group them (A-C (25 items)) and display them in columns.
Any idea how to get these into arrays like:
var listGroups[A] = the list items.
I would prefer to group them off the first letter of the list item and not the class="A", but whichever way gets the job done easiest will do.
Thanks in advance!
Upvotes: 3
Views: 1033
Reputation: 21528
If I understand you want to show your element grouping by its class. (in your html class is equal to the first letter)
In jquery you can do a multi class select in this way:
ul li {
display: none;
}
$(".A, .B, .C").show();
To get total group elements:
var l = $(".A, .B, .C").length;
console.log(l);
Take a look at the example.
To show in column you have to adjust css or you can take inspiration from this DEMO.
Upvotes: 0
Reputation: 1330
This stores each "category" in a different array (myArray
will contain a bunch of arrays)
and then you can decide to retrieve entries with myArray['A']
or myArray['B']
, etc which will allow you to group them easily.
This method doesn't require you to add class="A"
, class="B"
, etc as it's retrieving the text.
Javascript:
$(function(){
var myArray = [];
$.each($(".list-items li"), function(index, value){
var text = $(value).text();
var firstLetter = text.charAt(0);
if(!myArray[firstLetter]) myArray[firstLetter] = [];
myArray[firstLetter].push(text);
});
console.log(myArray);
});
Hope this helps!
Upvotes: 2
Reputation: 1495
i dont know if this is what are you looking for but try this :
Jquery :
var listGroups = {};
$('#1 li').each(function () {
var liClass = $(this).attr('class');
if (listGroups[liClass] === undefined) {
listGroups[liClass] = new Array();
}
listGroups[liClass].push(this.outerHTML);
});
what i dide here is i created an array of all the classes in the UL (A,B,C.....) and in evry one of them i put an array of the LIs that belongs to that class
Upvotes: 1