Reputation: 571
I have a list in my html code where onclick of parent checkbox, the immediate child checkboxes should be checked.
<ul class="test_ex">
<li>
<input type="checkbox" id="chckBox" class="parent" onchange="fnTest(this);" /> <a class="ref">Fruits</a>
<ul class="example">
<li>
<input type="checkbox" id="chckBox" class="child" /> <a class="ref"> Apple </a>
</li>
<li>
<input type="checkbox" id="chckBox" class="child" /> <a class="ref"> Orange </a>
</li>
</ul>
<ul class="test_ex_sample">
<li>
<input type="checkbox" id="chckBox" class="parent" onchange="fnTest(this);" /> <a class="ref">Birds</a>
<ul class="example">
<li>
<input type="checkbox" id="chckBox" class="child" /> <a class="ref"> Peacock </a>
</li>
<li>
<input type="checkbox" id="chckBox" class="child" /> <a class="ref">Parrot </a>
</li>
</ul>
<ul class="example">
<li>
<input type="checkbox" id="chckBox" class="parent" onchange="fnTest(this);" /> <a class="ref"> Food </a>
<ul class="example">
<li>
<input type="checkbox" id="chckBox" class="child" /> <a class="ref">Bread </a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
I tried many ways to check only the immediate children. But none of them worked. These are few ways:
Tried using .siblings() [Found this solution on stackoverflow only]
function fnTest(check) {
if ($(check).is(':checked')) {
$(check).siblings('.example').find('.child').attr("checked", true);
} else {
$(check).siblings('.example').find('.child').attr("checked", false);
}
}
Here the first list works properly. Again when clicked on second list it checks the 3rd list children too.
function fnTest(check) {
if ($(check).is(':checked')) {
var classParent = "." + $(check).attr('class');
$(classParent).attr("checked", true);
}
}
It would be great if someone could point out my mistake.
Upvotes: 3
Views: 9139
Reputation: 1
// OK tested
$(document).ready(function(e) {
$('input[type=checkbox]').click(function () {
$(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked'));
var sibs = false;
$(this).closest('ul').children('li').each(function () {
if($('input[type=checkbox]', this).is(':checked')) sibs=true;
})
$(this).parents('ul').prev().prop('checked', sibs);
});});
Upvotes: 0
Reputation: 698
From your first try of using siblings(). You can change to the following:
function fnTest(check){
if($(check).is(':checked')){
$(check).siblings('.example:first').find('.child').prop("checked",true);
}
else{
$(check).siblings('.example:first').find('.child').prop("checked",false);
}
}
By using this it checks only the first children. And also change your .attr to .prop
here is a demo : Fiddle
Upvotes: 2
Reputation: 9637
try
function fnTest(check) {
$(check).closest("ul").find(":checkbox").prop("checked",check.checked);
}
If you want only direct children use like this
function fnTest(check) {
$(check).closest("li").find(".example:first").find(":checkbox").prop("checked", check.checked);
}
NOTE : use latest version in jquery, and also id should be unique
Upvotes: 2
Reputation: 28523
You can do this by using jQuery. First of all remove onchange="fnTest(this);"
from parent checkbox. Bind change event for parent checkbox like below :
$(function(){
$('.parent').click(function(){
$(this).parent().find('.example:first .child').prop('checked',$(this).is(':checked'));
});
});
Upvotes: 2