I’d like to automatically uncheck or check children’s checkboxes:
HTML:
<ul>
<li>1 <input type='checkbox' /></li>
<ul>
<li>1.1 <input type='checkbox'' /></li>
</ul>
<ul>
<li>1.1.1 <input type='checkbox' /></li>
</ul>
</ul>
Javascript:
$('li input').change(function(){
if($(this).is(":checked")){
$(this).parent().next().children("li").children("input").attr("checked", true);
console.log("checked")
}else{
$(this).parent().next().children("li").children("input").attr("checked", false);
console.log("not checked")
}
})
But it only works for the first occurence. I don’t understand why because the 1.1 change and the event change is not triggered
Thanks
If you correct your HTML, to the following:
Then the following jQuery works:
JS Fiddle demo.
The problems with your HTML are that a
ulelement is not a valid child of anotherul, orol, element. The only valid children oful(andol) is thelielement, everything else is invalid, unless it’s contained within anli.References:
:checkboxselector.find().prop().