I think this is a bit of a logical problem.
Suppose you have a parent, a child, and a child if you click on an unverified child, the current element is checked, and the child of this, as well as the parent of this flag, but the sibling of this is not checked correctly?
What happens when you click on a parent? None of the checkboxes selected? Therefore, if I decided to check all the checkboxes, but I already have a checkbox for the child, does this mean that I will need to check the parent checkbox to undo everything, and then click it again to check all the checkboxes?
This is not a good user experience. Anyway, I think it's best to separate the click events function from three different listeners, rather than trying to do this in one function. With this in mind, I did not write out a use case when everything is checked for you, and you canceled the check of one of the child elements, and then check the parent checkbox. You will have to continue to expand them.
<ul class="level-one"> <li class="level-one-closed"> <span class="level-one-folder"> <input type="checkbox" class="userPermissionCheckBox-level-one" /> Parent </span> <ul class="level-two"> <li class="level-two-closed"> <span class="level-two-folder"> <input type="checkbox" class="userPermissionCheckBox-level-two" /> Child </span> <ul class="level-three"> <li> <span class="level-three-folder"> <input type="checkbox" class="userPermissionCheckBox-level-three" /> Child Child </span> </li> </ul> </li> <li class="level-two-closed"> <span class="level-two-folder"> <input type="checkbox" class="userPermissionCheckBox-level-two" /> Child </span> <ul class="level-three"> <li> <span class="level-three-folder"> <input type="checkbox" class="userPermissionCheckBox-level-three" /> Child Child </span> </li> <li> <span class="level-three-folder"> <input type="checkbox" class="userPermissionCheckBox-level-three" /> Child Child </span> </li> </ul> </li> </ul> </li> </ul> var $levelOneCheck = $('.userPermissionCheckBox-level-one'); var $levelTwoCheck = $('.userPermissionCheckBox-level-two'); var $levelThreeCheck = $('.userPermissionCheckBox-level-three'); $levelOneCheck.click(function() { var $isChecked = $(this).attr('isChecked'); if ($isChecked === 'true') { $(this).attr('isChecked', 'false'); $levelTwoCheck.prop('checked', false).attr('isChecked', 'false'); $levelThreeCheck.prop('checked', false).attr('isChecked', 'false'); } else { $(this).attr('isChecked', 'true'); $levelTwoCheck.prop('checked', true).attr('isChecked', 'true'); $levelThreeCheck.prop('checked', true).attr('isChecked', 'true'); } }); $levelTwoCheck.click(function() { var $isCheckedLevelTwo = $(this).attr('isChecked'); if ($isCheckedLevelTwo === 'true') { $(this).attr('isChecked', 'false'); $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', false).attr('isChecked', 'false'); $(this).closest('.level-two-closed').find('.level-three-folder .userPermissionCheckBox-level-three').prop('checked', false).attr('isChecked', 'false'); } else { $(this).attr('isChecked', 'true'); $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', true).attr('isChecked', 'true'); $(this).closest('.level-two-closed').find('.level-three-folder .userPermissionCheckBox-level-three').prop('checked', true).attr('isChecked', 'true'); } }); $levelThreeCheck.click(function() { var $isCheckedLevelTwo = $(this).attr('isChecked'); if ($isCheckedLevelTwo === 'true') { $(this).attr('isChecked', 'false'); $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', false).attr('isChecked', 'false'); $(this).closest('.level-two-closed').find('.level-two-folder .userPermissionCheckBox-level-two').prop('checked', false).attr('isChecked', 'false'); } else { $(this).attr('isChecked', 'true'); $(this).closest('.level-one-closed').find('.level-one-folder .userPermissionCheckBox-level-one').prop('checked', true).attr('isChecked', 'true'); $(this).closest('.level-two-closed').find('.level-two-folder .userPermissionCheckBox-level-two').prop('checked', true).attr('isChecked', 'true'); } });
http://jsfiddle.net/xzigraz/BTXNk/3/