Hi Team
I have check box for items got price range, the issue is the other selected catetory is not independent it affects other category. For instance when checked box for price range is select and unchecked it also hides instead to function on its own. How can i prevent this from happening?
Filter by price
$(document).ready(function() {
// Price checkboxes change event
$('input[type=checkbox][id^=price]').change(function() {
var $allCheckbox = $('#price-all');
var $checkedCheckboxes = $('input[type=checkbox][id^=price]:checked');
var $products = $('ul#product-list').find('li');
if ($checkedCheckboxes.length > 0 && !$allCheckbox.is(':checked')) {
$products.hide();
$checkedCheckboxes.each(function() {
var price = $(this).attr('id').split('-')[1];
$products.filter('[data-price="' + price + '"]').show();
});
} else if (!$allCheckbox.is(':checked')) {
$products.hide();
} else {
$products.show();
}
});
// Price all checkbox click event
$("#price-all").click(function() {
if($(this).is(':checked')) {
$(".custom-checkbox .badge").show();
} else {
$(".custom-checkbox .badge").hide();
}
});
});
Naimish MakwanaPosted May 8, 2023, 9:45 AM
The issue with the current implementation is that when any of the price range checkboxes are unchecked, it hides all the products that don't match the checked checkboxes including the products that belong to the categories that were selected through other checkboxes.
To prevent this from happening, you need to separate the filtering logic for the price range checkboxes from the logic for other checkboxes. You can achieve this by adding a separate filter function for the price range checkboxes.
Here's an updated JavaScript code that should solve the issue:
Thanks