Legazy Bali Legazy Bali - 7 months ago 46
Javascript Question

jQuery multiple filter data attributes

i need to filter data attribute using jQuery.
First i want to filter via selectbox and then filter with checkbox. All i want is when i checked both textbox, data will display all. But now, it only show the first data checkbox. Please see fiddle.
https://jsfiddle.net/legazy7891/t9krfjpy/4/

At if condition i've tried :

if ($('.3').is(':checked')) { chkBox.datatest = "1"; }
else if ($('.4').is(':checked')) { chkBox.datatest = "6"; }
else if ($('.3').is(':checked') && $('.4').is(':checked')) { chkBox.datatest = ""; }
else { chkBox.datatest = ""; }


But it doesn't work.

Reference : JQuery - Filtering data attributes with multiple filters of different inputs

Any help would be appreciate. Thanks

Answer

The issue is with the order of the condition. When you select both check box, the first condition will get satisfy $('.3').is(':checked') and chBox.dataset will get set to 1. It won't go to third condition.

Try following

if ($('.3').is(':checked') && $('.4').is(':checked')) {
   chkBox.datatest = "";
} else if ($('.3').is(':checked')) {
   chkBox.datatest = "1";
} else if ($('.4').is(':checked')) {
   chkBox.datatest = "6";
} else {
   chkBox.datatest = "";
}
Comments