Reputation: 81
Hello stackoverflow community. I need help with "too much recursion" error. It comes when I do these functions, weird, but everything works fine just error.:
function check_checker (siblings, status) {
if (siblings) {
if (status == true) {
$(siblings).children('li.imCheckbox').children('input').prop( "checked", true );
if ($(siblings).children('ul')) {
check_checker($(siblings).children('ul'), true);
}
} else {
$(siblings).children('li.imCheckbox').children('input').prop( "checked", false );
if ($(siblings).children('ul')) {
check_checker($(siblings).children('ul'), false);
}
}
}
}
$(document).ready(function(){
$('body').on('click', 'input[name=impTaskCh]', function(){
if ($(this).is(':checked')) {
var siblingas = $(this).parent().siblings('ul');
check_checker(siblingas, true);
} else {
var siblingas = $(this).parent().siblings('ul');
check_checker(siblingas, false);
}
});
});
When check is clicked, if ul has ul it checks all the checkbox'es. Maby check_checker never ends or something? What you guys think?
Upvotes: 1
Views: 353
Reputation: 1241
Yes this is never ending. $(siblings).children('ul')
will return an Object, which is truthy, so it will always be true. I would recommend using the length property instead.
function check_checker (siblings, status) {
if (siblings) {
if (status == true) {
$(siblings).children('li.imCheckbox').children('input').prop( "checked", true );
if ($(siblings).children('ul').length > 0) {
check_checker($(siblings).children('ul'), true);
}
} else {
$(siblings).children('li.imCheckbox').children('input').prop( "checked", false );
if ($(siblings).children('ul').length > 0) {
check_checker($(siblings).children('ul'), false);
}
}
}
}
Upvotes: 1