Can you do a better code? I need to check/uncheck all childs according to parent and when an child is checked, check parent, when all childs are unchecked uncheck parent.
$('.parent').children('input').click(function() { $(this).parent().siblings('input').attr('checked', this.checked); }); $('.parent').siblings('input').click(function() { if (this.checked) { $(this).siblings('div').children('input').attr('checked', true); return; } var childs = $(this).siblings('div').siblings('input'); for (i = 0; i < childs.length; i++) { if ($(childs.get(i)).attr('checked')) return; } $(this).parent().children('div').children('input').attr('checked', false); });
1 Answer