Justin Meli
Justin Meli

Reputation: 69

Checkbox Filter results

I am working on something similar to this.

What I want to add is that when '1 Bedroom' is selected, any items that have 1 Bedroom should display, even if none of the location checkboxes are selected.

Thanks.

Upvotes: 0

Views: 232

Answers (2)

tijs
tijs

Reputation: 566

Try this in your input change function:

var bedrooms = $(this).data('bedrooms');
$('ul > li').each(function(){
    if($(this).data('bedrooms') !== bedrooms) $(this).toggle();
});

Upvotes: 0

jaypeagi
jaypeagi

Reputation: 3141

All you need to do is add an "or" to your statement at the bottom which checks if the array is empty or not.

Keeping your current code structure, the last bit of you code would look like this:

$('li').each(function() {
    if (($.inArray($(this).data('location'), loc_array) > -1 || loc_array.length == 0) && ($.inArray($(this).data('bedrooms'), room_array) > -1 || room_array.length == 0)) {
        $(this).show();
    } else {
        $(this).hide();
    }
});

Of course your code could be re-factored to make it easier to read and (possibly) more efficient. But the purpose of this is to help you understand what you need to do. Re-factoring comes later!

Upvotes: 2

Related Questions