jquery - How to use javascript to filter table rows based on multiple sections of checkboxes -
i have following checkboxes in form allows me show/hide rows in table. each section of checkboxes works fine, how make 1 section check status of other section of checkboxes, logic filters based on selections?
i hope can see trying based on snippet of sample data:
$(document).ready(function() { $("#type :checkbox").click(function() { $("td").parent().hide(); $("#type :checkbox:checked").each(function() { $("." + $(this).val()).parent().show(); }); }); $("#fee :checkbox").click(function() { $("td").parent().hide(); $("#fee :checkbox:checked").each(function() { $("." + $(this).val()).parent().show(); }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form name="repaymentcalc" id="myform" action=""> <section id="type"> <p id="mortgage type">mortgage type</p> <input type="checkbox" name="type" value="t2" id="t2" checked/>2yr fixed <br> <input type="checkbox" name="type" value="t3" id="t3" checked/>3yr fixed <br> <input type="checkbox" name="type" value="t5" id="t5" checked/>5yr fixed <br> </section> <section id="fee"> <p id="fee">fee</p> <input type="checkbox" name="fee" value="fee" id="fee" checked/>fee <br> <input type="checkbox" name="fee" value="nofee" id="nofee" checked/>no fee <br> </section> </form> <table id="mortgagetable"> <tbody> <tr class="product"> <td class="t2">2yr fixed</td> <td class="initialrate">1.29</td> <td class="fee">999</td> </tr> <tr class="product"> <td class="t3">3yr fixed</td> <td class="initialrate">1.29</td> <td class="fee">999</td> </tr> <tr class="product"> <td class="t5">5yr fixed</td> <td class="initialrate">1.29</td> <td class="fee">999</td> </tr> <tr class="product"> <td class="t2">2yr fixed</td> <td class="initialrate">1.29</td> <td class="nofee">no fee</td> </tr> <tr class="product"> <td class="t3">3yr fixed</td> <td class="initialrate">1.29</td> <td class="nofee">no fee</td> </tr> <tr class="product"> <td class="t5">5yr fixed</td> <td class="initialrate">1.29</td> <td class="nofee">no fee</td> </tr> </tbody> </table>
merge both checkbox click handler similar below
$(":checkbox").click(function () { var typevals = [], feevals = []; $("tr").hide(); $("#type :checkbox:checked").each(function () { typevals.push($(this).val()); }); $("#fee :checkbox:checked").each(function () { feevals.push($(this).val()); }); (var = 0; < typevals.length; i++) { (var j = 0; j < feevals.length; j++) { $('td.' + typevals[i] + '~ td.' + feevals[j]).closest('tr').show(); } } });
Comments
Post a Comment