javascript - Keeping a checkbox checked with local storage but also filter a list on load -
alright, is/was 2 part question, posted function:
$(function () { var data = localstorage.getitem("filter-by"); if (data !== null) { $("input[name='filters']").attr("checked", "checked"); } }); $("input[name='filters']").click(function () { if ($(this).is(":checked")) { localstorage.setitem("filter-by", $(this).val()); } else { localstorage.removeitem("filters"); } });
as start re-checking checkbox after load using local storage. after trial , error able me write new function:
$("input[name='filters']").click(function () { var items = localstorage.getitem("filter-by") || []; var data = this.data('filter-by'); if ($(this).is(":checked")) { items.push(data); } else if (items.indexof(data) >= 0) { items.splice(items.indexof(data), 1); } if (items.length > 0) { localstorage.setitem("filter-by", items); } else { localstorage.removeitem("filter-by"); } }); $(function () { var items = localstorage.getitem("filter-by") || []; $("input[name='filters']").each(function(index, input) { var data = $(input).data('filter-by'); if (items.indexof(data) >= 0) { $(input).attr('checked', 'checked'); } }); });
with function, i'm able store checkboxes checked, list not filter. how can go making list filter on refresh well, using either of functions?
i using input looks this:
<input type="checkbox" name="filters" data-filter-by="brand a" ng-click="includebrand('brand a')" />brand
thanks guys!
either have trigger click event or need call includebrand(filterstring) function each of checked items on page load.
Comments
Post a Comment