1

I'm trying to select multiple checkboxes inside a div, if a specific checkbox is selected. Here's what I'm trying to do http://jsfiddle.net/8PLH6/

I'm trying to make this work. If I check "All" all the below checkboxes should be selected. For some reason I can't get the selectors right. Code:

<div style="float:left;padding-right:15px;">
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
    <input type="checkbox" value="1" id="all"/><a href="#" title="All">All</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
    <input type="checkbox" value="1" id="cat417"/><a href="" title="1">1</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
    <input type="checkbox" value="1" id="cat410"/><a href="" title="2">2</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
    <input type="checkbox" value="1" id="cat401"/><a href="" title="3">3</a>
</div>
<div align="left" style="padding-bottom: 4px; padding-left: 6px;" class="bottom">
    <input type="checkbox" value="1" id="cat415"/><a href="" title="4">4</a>
</div>
</div>

and

$(document).ready(function () {
$('#all').change(function () {
    $a = $(this).parent('div').parent('div').children('div').children('input[type="checkbox"]');
    $b = $(this).attr('checked');
    if ($b) {
        $($a).attr('checked', true);
    } else {
        $($a).attr('checked', false);
    }
});

});

1 Answer 1

2

This can be very tricky. Please use the .prop() function.

$(document).ready(function()
{
    $('#all').on("click",function () {
          $("input[type='checkbox']").not("#all").each(function() 
       {
           $(this).prop("checked",$("#all").is(":checked"));
        });
    });
});

jsFiddle: http://jsfiddle.net/8PLH6/3/

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

1 Comment

@Haniet Thanks - that's exactly what I wanted. However if I have another div with checkboxes, they become selected, too. I wish I could prevent that. See what I mean jsfiddle.net/8PLH6/4

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.