Yesterday i was giving an answer to one of the stackoverflow question that How to check/uncheck all checkboxes based on the selection of one checkbox
and i came up with this solution. But the problem is it only works once but it will not work if you click it twice. FIDDLE
$('#allcb').click(function(){
if ($('#allcb').is(':checked'))
{
$('.chk').each(function(){
$(this).attr('checked', true);
});
}
else
{
$('.chk').each(function(){
$(this).attr('checked', false);
});
}
});
I found this code which works fine according to the requirements FIDDLE
So what is wrong with my approach? Either it should not work at all. If it works once why its not works twice?