Pomster
Pomster

Reputation: 15197

Checking and uncheck check box's with class. Fails after first uncheck

I have a group of checkbox's and one of them is marked as all. If this is clicked i would like to check all the other checkbox's within this class, and if it is then clicked again i want to remove the check form all the checkbox's within that class.

At the moment when i check all, all my checkbox's are checked, then when i click all again, all the checkbox's become unchecked. If i try click all again only the all check box is clicked.

When the all check box is checked i run:

$('.chIndustry').attr('checked', 'checked');

When the all checkbox becomes unchecked:

$('.chIndustry').removeAttr('checked');

Upvotes: 0

Views: 83

Answers (2)

Raja Sekar
Raja Sekar

Reputation: 2130

$(document).ready(function() {
    $(".checkall").change(function() {
        console.log($(this).is(':checked'))
        if ($(this).is(':checked')) {
            $('input[name="test"]').prop('checked', true);
        } else {
            $('input[name="test"]').prop('checked',false );
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<input type="checkbox" name="test" class="test" />
<input type="checkbox" name="test" class="test" />
<input type="checkbox" name="test" class="test" />
<input type="checkbox" name="test" class="test" />
<input type="checkbox" name="test" class="test" /></br/>
Check all    <input type="checkbox"  class="checkall" />
The above works as per your requirement..

Upvotes: 1

Sam1604
Sam1604

Reputation: 1489

Try .prop to uncheck the check the check box

$('.chIndustry').prop('checked', false);

Upvotes: 1

Related Questions