Original:
var count = $('div.items:hidden').length;
New:
var counts = {}; $('#skills1,#skills2').each( function() { counts[$(this).attr('id')] = $('div.items:hidden', $(this)).length; }
The counts object will be:
{ skills1 : 3, skills2 : 5 }
Last (?)
$('#skills1,#skills2').each( function() { if ($('div.items:hidden',$(this)).length == 0) { $(this).hide(); } });
It would be even easier if you gave the โcontainersโ a class to use as a selector. Then you can simply replace #skills1,#skills2 with a class selector and not have a function depending on the DIV names, which means that you wonโt need to change it if you add another container, say skills3 . You just need to make sure this container matches the class.
source share