0

I have a few checkboxes that I would like to do a 'Select All' option for. Id or name for those checkboxes is 'chk1', 'chk2', chk3..... and the id for the selector checkbox is 'SelectAll'

    $(document).ready(function () {
        $('#SelectAll').click(function () {
             $('input[name^="chk"]').attr("checked", this.checked);
        })
    })

the code works fine the first time I select and unselect but doesn't work after the subsequent clicks.. pls help.

almavrick
  • 13
  • 1
  • 1
    Try using [`prop()`](http://api.jquery.com/prop/) instead - `$('input[name^="chk"]').prop('checked', this.checked);` – Mottie May 26 '13 at 16:12

1 Answers1

2
$(document).ready(function () {
    $('#SelectAll').on('change', function() {
        $('input[name^="chk"]').prop("checked", this.checked);
    });
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • @roasted - change would be the proper event for when a checkbox changes, even if it's changed with the keyboard, clicking a label etc. – adeneo May 26 '13 at 16:17
  • Look like the problem was attr. @adenao, you are right, change should be use in all case, i was just wondering – A. Wolff May 26 '13 at 16:18