Question regarding parent/child relationships with dynamically generated checkboxes using jquery

Posted by Jeff on Stack Overflow See other posts from Stack Overflow or by Jeff
Published on 2010-06-01T16:29:12Z Indexed on 2010/06/01 16:33 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

I have a form of checkboxes, that is dynamically generated based on the users content. Sections of checkboxes are broken up by categories, and each category has projects within. For database purposes, the category values have checkboxes, that are hidden. IF a category has sub items that have checkboxes that are checked, THEN the category checkbox is checked as well.

I have gotten this working ok using the JQuery .click(), but I can't figure out how to do it when the page loads.

Here is my code for when a checkbox is actually clicked:

$(".project").click(function() {

    if($(this).is(":checked") && $(this).hasClass("project")) {
       $(this).parent().parent().children(':first').attr('checked', true);

    }else if(!$(this).parent().children('.project').is(":checked")){
        $(this).parent().parent().children(':first').attr('checked', false);

    }
 });

Now when I am editing the status of these boxes (meaning after they have been saved to the db) the category boxes are not showing up as checked even though their children projects are checked. What can I do to make it so that my category box will be checked at load time if that category's child is checked?

Part of the problem I think is with the dynamically changing parent child setup, how can I find the parent box in order to have it checked? Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about checkbox