Use JQuery to check a checkbox in a parent list-item?

Posted by Daniel O'Connor on Stack Overflow See other posts from Stack Overflow or by Daniel O'Connor
Published on 2011-01-05T02:50:03Z Indexed on 2011/01/05 2:54 UTC
Read the original article Hit count: 200

Filed under:
|
|
|

Hey Everyone,

I'm brand new to Javascript and JQuery, so I've been reading up on it and am trying to check (and set inactive) a checkbox in a parent list-item when one of the children are checked.

If this doesn't make any sense, take a look at the list structure.

<ul>
    <li><input type="checkbox" name="items[]" value="88712003" id="88712003"  /> Parent 1</li>
    <li><input type="checkbox" name="items[]" value="88712003" id="88712003"  /> Parent 2
        <ul>
            <li><input type="checkbox" name="items[]" value="21312341" id="21312341" /> Child 1</li>
            <li><input type="checkbox" name="items[]" value="21312341" id="21312341" /> Child 2</li>
        </ul>
    </li>
    <li><input type="checkbox" name="items[]" value="88712003" id="88712003"  /> Parent 3</li>
    <li><input type="checkbox" name="items[]" value="88712003" id="88712003"  /> Parent 4</li>
</ul>

If Child 1 or Child 2 is checked, I want the input in Parent 2 to be checked and set inactive. I've started working on it, but got stuck here:

$(function(){
    $('.child').click(function() {
        $(this).parent().parent().parent().toggle();
    });
});

As you can see, I didn't make it far. Any help would be appreciated.

Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about list