irfanmcsd
irfanmcsd

Reputation: 6571

check all code not working on Jquery 2.0

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

Answers (2)

Dan P.
Dan P.

Reputation: 1431

Try doing something like this:

$("#selectall").on('click', function () {
   $('.case').prop('checked', $(this).prop('checked'));
});

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

use Jquery.prop() instead of Jquery.attr()

Upvotes: 3

Related Questions