Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tree menu, checking / uncheching all child items, client side, help !

Status
Not open for further replies.

kigoobe

Programmer
Dec 25, 2007
6
FR
Hi, I'm trying to create a javascript function that will check / uncheck checkboxes associated with a multilevel tree menu. I've uploaded the page in where the menu is created using php.

As you see now, when we click a menu, not all / none of its child menus are getting checked / unchecked. In somd cases, check boxes are not even clickable.

The function as it is now -

------------------------------------------------
function getChildren(id) {
var children = [];
for (var i=1;i <= menuArray.length; i++) {
var parentid = menuArray;
if (parentid == id) {
children.push(i);
children.concat(getChildren(i));
var obj=document.getElementById("veh_"+id);
obj.checked = (obj.checked == true) ? false : true;
}
}
return children;
}
---------------------------------------------------------

Thanks for any help.
Best.
 
Your logic is a bit wrong for selecting / deselecting child checkboxes based upon the state of the parent.

This works for me in both IE 6 and Fx 2:
Code:
function getChildren(id) {
	for (var loop=id + 1; loop<menuArray.length; loop++) {
		if (menuArray[loop] == id) {
			var parentObj = document.getElementById('veh_' + id);
			var obj = document.getElementById('veh_' + loop);
			obj.checked = parentObj.checked;
		}
	}
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan

Thanks a lot for your reply. I really appreciate this. Just one thing friend. While this works wonderfully, this just works upto one level. In case when we have multi level of children, this is not going deep and getting limited to one level only.

Will this be possible to extend this function in a way that it goes deeper, and can check / uncheck all children (including the parent, as it is now) till the deepest level?

Thanks again for your valuable input.
Best.

 
Aaah - that's because you implied only one level with the phrase "child menus" instead of "descendant menus", which would have encompassed children, children of children, etc.

You should have no problems calling getChildren recursively after the "obj.checked = parentObj.checked;" line, although personally, I'd rethink your whole data structure and slightly archaic way of delivering all this data to the page - a quick re-factor would make this whole exercise a lot easier.

Hope this helps,
Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You should have no problems calling getChildren recursively after the "obj.checked = parentObj.checked;" line, although personally, I'd rethink your whole data structure and slightly archaic way of delivering all this data to the page - a quick re-factor would make this whole exercise a lot easier.

thanks for the fast reply Dan :) But could you explain this a bit please? I'm not good in JS, you might have guessed that already ... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top