i have the following snippet of javascript:

liChilds[x].onclick = function(){
toggleSubMenu(sub_menu_id, false);
return false;
That code happens within a loop, and sub_menu_id has a different value through each iteration of the loop. For example, if it loops 4 times, then there will be 4 onclick functions and sub_menu_id would change for each one. This should happen in theory, but instead, whichever item you click on, sub_menu_id takes the value of its last value in the loop. (4 in this example)

My question is, how do I make the function toggleSubMenu take the value of sub_menu_id at the time the on click function is created (i.e while looping) rather than taking the value of sub_menu_id when it is actually clicked.