2

I am using jQuery to handle selecting all checkboxes for a form. Here's my code:

    $(function () {
        $('#selectall').on('click', function() {
            $('.selectedId').attr('checked', $(this).is(":checked"));       
        });
    });

    <input type="checkbox" id="selectall">Select all</input>
    <input type="checkbox" name="industry1" id="industry1" value="1" class="selectedId">
    <input type="checkbox" name="industry2" id="industry2" value="1" class="selectedId">

When I check the selectall id checkbox, it selects all of the checkboxes with the class selectedId. When I click it again, it removes the checks against all.

My problem is though, after that initial select all/deselect all it no longer operates, as in I cannot select all.

Any idea what I should be doing in my javascript that will fix this issue?

Thanks!

somejkuser
  • 8,856
  • 20
  • 64
  • 130
  • 1
    Would you like a weird answer to go with your weird action ? – adeneo Apr 01 '14 at 20:26
  • possible duplicate of [check uncheck All checkboxes with another single checkbox use jquery](http://stackoverflow.com/questions/15504643/check-uncheck-all-checkboxes-with-another-single-checkbox-use-jquery) – epascarello Apr 01 '14 at 20:28

1 Answers1

3

Use the checkbox properties instead of their attributes

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

http://jsfiddle.net/VmnE2/

Musa
  • 96,336
  • 17
  • 118
  • 137