1

I have a checkboxes that are being stored in a cookie.

Here is the jquery code:

//JQuery that will set the checkbox in it's current state
 $("#checkAll").on("change", function() {
    $(':checkbox').not(this).prop('checked', this.checked);
  });

  $(":checkbox").on("change", function(){
    var checkboxValues = {};
    $(":checkbox").each(function(){
      checkboxValues[this.id] = this.checked;
    });
    $.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' })
  });

  function repopulateCheckboxes(){
    var checkboxValues = $.cookie('checkboxValues');
    if(checkboxValues){
      Object.keys(checkboxValues).forEach(function(element) {
        var checked = checkboxValues[element];
        $("#" + element).prop('checked', checked);
      });
    }
  }

  $.cookie.json = true;
  repopulateCheckboxes();

The code above works perfectly but when I try to use a button to uncheck them it does not uncheck at all.

Here is the function that I used:

 $("#UncheckAll").click(function(){
   $("input[type='checkbox']").prop('checked',false);
});

What should I do? could somebody help me.

user3230425
  • 695
  • 2
  • 7
  • 12

2 Answers2

1

Give all checkboxes a class such as myclass. Then using jQuery.

Remove your cookie in document.ready function

 $.cookie("checkboxValues", null);//checkboxValues is the name of your cookie

For jQuery 1.6+

$("#UncheckAll").click(function(){
   $('input:checkbox:checked.myclass').prop('checked',false);
});

for jquery 1.5-

$("#UncheckAll").click(function(){
   $('input:checkbox:checked.myclass').attr('checked',false);
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mir Gulam Sarwar
  • 2,588
  • 2
  • 25
  • 39
0

try this

 $("#UncheckAll").click(function(){
   $('input:checkbox.class').prop('checked',false);
});

or this if not have common class

 $("#UncheckAll").click(function(){
   $(':checkbox').prop('checked',false);
});
Tarun
  • 166
  • 10