The very first function sets up the main menu and links to other menus/pages.
Note I have written addEvent, and it is self explanatory and has been tested and works.
Expand|Select|Wrap|Line Numbers
- var buttons = ["MenuX", "MenuY", "MenuZ", "MenuZZ"];
- var menu_page_generators = [
- function(m, mel){// Main Menu
- for(var id=0;id<buttons.length;id++){
- var t = document.createElement("div");
- t.className = "menu-button";
- t.appendChild(document.createTextNode("["+buttons[id]+"]"));
- var func = function(){alert(id);};
- addEvent(t, "click", func);
- mel.appendChild(t);
- }
- },
- function(m, mel){// ...
- alert('hi2');
- }
- ];
Now to the problem. It names each of the buttons correctly, so the 'id' variable is obviously being applied correctly in the button creation lines. However when you click on the individual buttons they all alert '4'. So it is obvious that all the buttons are not taking the 'id' variable when the function is being created, but 'id' when the button is clicked (after the loop has completed and id=4).
I do not understand why it is doing this? I would really like to get this fixed, any help would be much apreciated.
Cheers, Josh