<div id="tree">
<ul>
<li>
<input type="checkbox"/> Root
<ul>
<li>
<input type="checkbox"/> Child 1
<ul>
<li><input type="checkbox"/> Subchild 1</li>
<li><input type="checkbox"/> Subchild 2</li>
<li><input type="checkbox"/> Subchild 3</li>
</ul>
</li>
<li>
<input type="checkbox"/> Child 2
<ul>
<li><input type="checkbox"/> Subchild 1</li>
<li><input type="checkbox"/> Subchild 2</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Above is html code where there is a tree with parent and child nodes.
This works well. But if all the sub node are checked then root/parent has to be automatically checked.
And also if all the sub node are unchecked then root/parent has to be automatically unchecked. How do I achieve this?
Jquery
$(document).ready(function() {
$("#tree input:checkbox").change(function() {
var val = $(this).attr("checked");
$(this).parent().find("input:checkbox").each(function() {
if (val) {
$(this).attr("checked", "checked");
}
else {
$(this).removeAttr("checked");
}
});
});
});