I'm using the following html/jquery to re-order the child elements of a containing div, based on their data-price attribute.
<div id="parent_div">
<div class="child_div" data-price="102">Item x</div>
<div class="child_div" data-price="95">Item y</div>
<div class="child_div" data-price="210">Item z</div>
</div>
The HTML is populated by multiple AJAX calls and so the initial results are not ordered, and could number 50+ child elements. Once the AJAX calls are complete and HTML populated, I am running the following jquery:
function giveFormatOrder(parent_div, child_div) {
var $wrapper = $(parent_div);
$wrapper.find(child_div).sort(function(a, b) {
return +a.getAttribute('data-price') - +b.getAttribute('data-price');
})
.appendTo($wrapper);
}
giveFormatOrder('#parent_div','.child_div');
This works fine. But I would like to hide all but the first 10 ordered child elements. Is there a way I can modify the jquery function to achieve this, by adding a counter and adding a show/hide class? Or do I need to trigger a second function once the sort is complete? I'm looking for the most performant approach possible but am not sure how to go about it.