I have three sets of checkboxes which allow me to filter a number of divs based on data attributes. This works great but not as I need it to.
If you visit the jsfiddle below you will see a working test.
What I need it to do is:
When more than one option is chosen within the same category e.g. 'small' and 'medium', it should increase the results i.e. show all 'small' and 'medium' results, (which is what it does at the moment).
When more than one option is chosen within different categories e.g. 'medium' and 'north america' it should reduce the results i.e. show all 'medium' flowers in 'north america'
How would I go about fixing/amending my code to make this work please? (I hope this makes sense).
Below is the jquery I'm using:
$('.flowers-wrap, .continents-wrap').delegate('input[type=checkbox]', 'change', function() {
var $lis = $('.flowers > div'),
$checked = $('input:checked');
if ($checked.length) {
var selector = '';
$($checked).each(function(index, element){
if(selector === '') {
selector += "[data-category~='" + element.id + "']";
} else {
selector += ",[data-category~='" + element.id + "']";
}
});
$lis.hide();
console.log(selector);
$('.flowers > div').filter(selector).show();
} else {
$lis.show();
}
});
Please see the jsfiddle I have setup to demonstrate the issue I'm having - http://jsfiddle.net/n3EmN/
namęproperty of the checkboxes, and when change occurs loop through group of checkboxes by theirnameproperty.