Help to modify the code! I'm just learning, don't scold too much ...

  1. there are checkboxes - id1 ... id6, as well as value, which can change

we get the values ​​of id and value:

var id = this.getAttribute('id'); var value = this.getAttribute('value'); 

the task is to check the checkboxes, if the mark is set, make a selection - categoryFilter('other'+ id, value); if there is no mark - categoryFilter('other'+ id,'');

everything works, but when you refresh the page, the sample is saved, and the checkbox doesn’t have any marks, as I understand it, you need to use a cookie, then I’ve got a stub ... help the beginner

I do not understand how to save data and how to use it later !?

 jQuery(document).ready(function (){ $('input[type="checkbox"]').on('change', function() { var id = this.getAttribute('id'); var value = this.getAttribute('value'); if(id == undefined) return; if(this.checked) { categoryFilter('other' + id, value); } else { categoryFilter('other' + id,''); } }); }); 
  • one
    @ Dimastik86, aren't you tired yet? Would pay $ 10 to the programmer, he would have done everything for you a long time ago. - Zhukov Roman
  • do for 10 bucks? I agree! - Dimastik86
  • @ Dimastik86, just saving checkboxes in cookies and returning them back? Agree 8) - Zhukov Roman
  • not only)) there will be several more points)) - Dimastik86

2 answers 2

Something like this:

 $(document).ready(function (){ var isCheck = false; for (i=0; i<10; i++){ isCheck = (isset($.cookie("check"+i) && $.cookie("check"+i) != '')?$.cookie("check"+i):false; if (isCheck){ $('#isCheck'+i). attr('checked','checked'); }else{ $('#isCheck'+i). removeAttr('checked') ; } } $('input[type="checkbox"]').on('change', function() { var id = this.attr('id'); var value = this.val(); if(id == undefined) return; if(':checked') ) { categoryFilter('other' + id, value); $.cookie('check' + id, value); } else { categoryFilter('other' + id,''); $.cookie('check' + id, ''); } }); }); 
    1. For cookies, you can connect the plugin with jQuery:

      jQuery Cookie Plugin v1.3

    2. To add a value: jQuery.cookie('nameCook', 'value'); for example, you can insert the following in the name of cookies: 'check'+id , and in 'value' the value variable itself, i.e. the following will already be in the check1=true , check2=false , check3=true : check1=true , check2=false , check3=true ...

    3. When loading, we read the values ​​from the cookie, it is possible in a loop. And literally: if check1 exists, then we set checked = check1 ...

       for (i=0; i<10; i++) { isCheck = jQuery.cookie("check"+i); // забираем значение из кук } 

    instead of a loop, you can simply by id get the value of jQuery.cookie ("check" + id); Here is an approximate algorithm for your question)