I am trying to make a list of products filtered using checkboxes and jQuery. I have some code working (thanks to the previous answers I found here).
I want to use CSS classes to show or hide elements.
I am trying to filter elements by color, outline and price, and the problem is that my code is currently selecting elements using OR between different filter groups.
I need to be able to filter using OR in each group (e.g. color), but with AND when the checkboxes are in different groups. The way I want to do this is to add '.' between class names, therefore, the element will match the filter only if the color classes AND and AND and the css price match.
example # div.dark.smooth.b
I tried this in the Firebug console and I can filter the elements I need in this way, but, unfortunately, I do not know how to do this in jQuery. My code is below ...
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body> <ul id="colour"> <li><input type="checkbox" name="colour" value="dark"> Dark</li> <li><input type="checkbox" name="colour" value="medium"> Medium</li> <li><input type="checkbox" name="colour" value="light"> Light</li> </ul> <ul id="finish"> <li><input type="checkbox" name="finish" value="smooth"> Smooth</li> <li><input type="checkbox" name="finish" value="riven"> Riven</li> <li><input type="checkbox" name="finish" value="honed"> Honed</li> </ul> <ul id="price"> <li><input type="checkbox" name="price" value="a"> Up to £25</li> <li><input type="checkbox" name="price" value="b"> £25 to £45</li> <li><input type="checkbox" name="price" value="c"> £45 to £65</li> <li><input type="checkbox" name="price" value="d"> £65 to £85</li> <li><input type="checkbox" name="price" value="e"> over £85</li> </ul> <div class="dark smooth b">dark smooth b</div> <div class="medium honed d">medium honed d</div> <div class="dark smooth d">dark smooth d</div> <div class="light smooth b">light smooth b</div> <div class="light riven b">light riven b</div> <div class="dark riven c">dark riven c</div> <div class="medium riven a">medium riven a</div> <script> $("#colour :checkbox,#finish :checkbox,#price :checkbox").click(function() { $("div").hide(); $("#colour :checkbox:checked").each(function() { $("." + $(this).val()).slideDown('slow'); }); $("#finish :checkbox:checked").each(function() { $("." + $(this).val()).slideDown('slow'); }); $("#price :checkbox:checked").each(function() { $("." + $(this).val()).slideDown('slow'); }); }); </script> </body> </html>
source share