Organizing code into separate functions for better readability and accessibility can greatly improve the efficiency of a program. By creating a single function that can be easily called multiple times, we can ensure that our code is reusable and responsive to different events.
ToggleThings();
$("#filters :checkbox").change(function() {
ToggleThings();
});
function ToggleThings()
{
$(".wpbdp-listing").hide();
$("#filters :checkbox:checked").each(function() {
$('.' + $(this).val()).closest('.wpbdp-listing').show();
});
}
http://jsfiddle.net/p9s0pdao/1/
Update (final solution):
ToggleThings();
$("#filters :checkbox").change(function() {
ToggleThings();
});
function ToggleThings()
{
var checkedboxes = $("#filters :checkbox:checked");
if (checkedboxes.length > 0)
{
$(".wpbdp-listing:visible").hide();
checkedboxes.each(function() {
$('.' + $(this).val()).closest('.wpbdp-listing').show();
});
}
else
{
$(".wpbdp-listing").show();
}
}
http://jsfiddle.net/p9s0pdao/4/