Reputation: 6571
I am writing the following jquery code to select / unselect checkboxes. it works fine using jquery version 1.7.2 but failed while upgrading to jquery 2.0.*
Code:
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
I also used this approach but it failed too.
$('#selectall').click(function() {
$('.case').each(function() {
$(this).attr('checked',!$(this).attr('checked'));
});
});
Sample html output
<input type="checkbox" id="selectall"> select all
.....
<input type="checkbox" class="case" value="1">
...
...
<input type="checkbox" class="case" value="5">
Note the following code failed on second attempt while checking / unchecking select all checkbox.
Upvotes: 0
Views: 112
Reputation: 1431
Try doing something like this:
$("#selectall").on('click', function () {
$('.case').prop('checked', $(this).prop('checked'));
});
Upvotes: 0