4

When I click on Problem, both check boxes must select/deselect. I want to select and deselect check boxes for multiple time. It works only for selecting and then deselecting (2 times) and then is not working any more.

check boxes

My code:

 $(document).ready(function() {
        $('#CHECK-ALL').click(function() {
            if ($(this).is(':checked')) {
                $('#P').attr('checked',true);                
            } else {
                $('#P').attr('checked',false);                
            }
        });
    });
0

1 Answer 1

15

You're using the wrong method, you should be using prop

$(document).ready(function() {
    $('#CHECK-ALL').click(function() {
        if ($(this).is(':checked')) {
            $('#P').prop('checked',true);                
        } else {
            $('#P').prop('checked',false);                
        }
    });
});

Your code could be reduced to

$('#CHECK-ALL').click(function() {
    $('#P').prop('checked', this.checked);                
});

When you set the attribute, it works fine, but it doesn't change the property, so the next time you check $(this).is(':checked') it's still unchecked, as that checks the property, not the attribute, which is why it's not working.

Sign up to request clarification or add additional context in comments.

4 Comments

How would I set the attribute to checked="checked", because now it's not?
The attribute shouldn't matter, the property is set, which is what is important
When you send form to PHP file or controller, this value is then marked as "checked" right?
Yes, when you submit the form it's the property that counts, not the attribute

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.