0

I need to check one checkbox (checkAll) and select all and if I deselect it all should be deselected too. Additionally, if they are all selected and then I uncheck 'a', 'b' or 'c', 'checkAll' also need to be unchecked.

<input type="checkbox" value="" id="checkAll">
<input type="checkbox" value="a">
<input type="checkbox" value="b">
<input type="checkbox" value="c">

What's the trigger I need to use? Tried but failed:

$("#checkAll").click(function() {
    alert('test');
});

4 Answers 4

2

Use the change event. When the #checkAll box is changed, change the other boxes correspondingly.

To change the #checkAll box when according to the others, you could do something like this:

$(":checkbox").not("#checkAll").change(function()
{
    $("#checkAll").attr("checked", $(":checkbox").not(":checked").not("#checkAll").length ? false : true);
});

Basically this will execute when a checkbox other than #checkAll changes, and change #checkAll depending on whether or not there are unchecked boxes.

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

Comments

2

Simple script that does what you want:

HTML:

<div>
    <input type="checkbox" value="" id="checkAll">
    <input type="checkbox" value="a" class="others">
    <input type="checkbox" value="b" class="others">
    <input type="checkbox" value="c" class="others">
</div>

javascript:

<script type="text/javascript">
    $("#checkAll").change(function() {
        $(":checkbox").attr("checked", this.checked);
    });
    $(".others").change(function() {
        if (!$('input.others[type=checkbox]:not(:checked)').length)
            $("#checkAll").attr('checked', true);
        if (!$('input.others[type=checkbox]:checked').length)
            $("#checkAll").attr('checked', false);
    });
</script>

2 Comments

This works to check and uncheck all checkboxes if I click "checkAll". However, if all are checked and I uncheck of of the "others" it should also uncheck "checkAll". Also if "checkAll" is not selected and I mannually select all "others" it should select "checkAll". Sorry, it sounds convoluted a bit.
This works fine for me. What brower and jquery version are you using? Do you use the script multiple times? (in this case, it doesn't work, but i can rearange it to work.)
0

I didn't change any of your code and it works for me: http://jsfiddle.net/9U2aQ/

Make sure that you're referencing your jquery file correctly.

2 Comments

This 'answer' does not address @santa's needs at all. I'd downvote if I had votes left today.
@santa's need is to understand why his/her code isn't working. I have verified that the code works and provided a recommendation as to why it's not working in his/her environment.
0

This should work:

    $(':checkbox').not('#checkAll').change(function()
    {
        $("#checkAll").attr("checked", $(":checkbox").not(":checked").not("#checkAll").length ? false : true);
    }); 
    $("#checkAll").change(function() 
    {
        $(':checkbox').attr('checked', this.checked);
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.