I have a list of elements. Upon clicking a link, I want to filter the elements by a class. (so far it's okay). The trouble: if there are no elements with that class, I want to print a message saying that.
$('.filterEventsLinks a').click(function(){
// count total number of events
var evCount = $('.singleEvent').length;
// a list of comma-separated class names to filter are stored in the data attribute of the fired element
// for example: <a data-category="kids,teens,adults" href="#"></a>
var cats = $(this).data('category');
// split into an array
var catArr = cats.split(',');
// loop through
for(var i in catArr) {
var cat = '.CAT-' + catArr[i];
// hide things that do not have this category
$('.singleEvent').not(cat).slideUp(200);
// show things that do
$(cat).slideDown(200);
}
// count number of remaining visible elements
var evCount = $('.singleEvent').filter(':visible').length;
// do more...
});
Everything here works as expected except that last line. The last line that doesn't seem to work. It always returns the number of elements that exist, not the number that are currently visible.
Same problem listed in this SO question: Count number of elements in list after slideup/slidedown