I am using this script to check and uncheck all checkboxes:
$('#checkall').click(function () {
var checked = $(this).data('checked');
$('.chkall').find(':checkbox').attr('checked', !checked);
$(this).data('checked', !checked);
});
It works great, but as soon as I uncheck several checked checkboxes after "check all" and then hit "uncheck all" and "check all" again, those previously unchecked checkboxes are not checked again. Help would be great! Thank you very much!
I guessed what your HTML looks like, in terms of the triggering button, but what I don't know is how you initially set the
data
on it. hope this is the way you have it coded. if not, please tell.This might be the correct way..
Use prop instead of
attr
and group the checkbox list inside a div, just for organizing purposes. Also use thechecked
as it is, don't negate it.You simply need to get all checkboxes and check them all.