473,498 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with simple iteration

Jezternz
145 New Member
Okay, so basicly I have an array of functions, these functions are used to setup different menu's.
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
  1. var buttons = ["MenuX", "MenuY", "MenuZ", "MenuZZ"];
  2. var menu_page_generators = [
  3.         function(m, mel){// Main Menu
  4.             for(var id=0;id<buttons.length;id++){
  5.                 var t = document.createElement("div");
  6.                 t.className = "menu-button";
  7.                 t.appendChild(document.createTextNode("["+buttons[id]+"]"));
  8.                 var func = function(){alert(id);};
  9.                 addEvent(t, "click", func);
  10.                 mel.appendChild(t);
  11.             }
  12.         },
  13.         function(m, mel){// ...
  14.             alert('hi2');
  15.         }
  16.     ];
  17.  
So what this code should be doing is. It should create 4 buttons, each button named MenuX, MenuY, ect and when you click on button X it should alert "1", button Y should alert "2", ect

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
Dec 8 '09 #1
2 1664
Dormilich
8,658 Recognized Expert Moderator Expert
you unintentionally formed a Closure on line 8.

to get out of this you can write the value to a property of the element and access that via this. (that’s the only thing I can remember right now)
Dec 8 '09 #2
gits
5,390 Recognized Expert Moderator Expert
i don't know whether the closure is unintentionally - it just closures the last value of id (it is an issue with for-loops) ... so when you create a 'real' or 'explicit' closure (like below) it should work:
Expand|Select|Wrap|Line Numbers
  1. var func = function(val) {
  2.     return function() { alert(val) };
  3. }(id);
kind regards
Dec 8 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4605
by: Sulla | last post by:
I ran into an interesting "problem" the other day when I was working on a very simple zip code utility function. I have a table in Oracle with the following columns: zipcode, state, latitude,...
45
2977
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
19
3784
by: snowdy | last post by:
I am using Interactive C with my handboard (68HC11) development system but I've got a problem that I am asking for help with. I am not new to C but learning all the time and I just cant see how to...
5
13594
by: Jean-François Michaud | last post by:
Hello people, I am rather puzzled by this problem I am having with doubles here. The code seems to execute correctly for awhile and then all a sudden printfing out my doubles, I get -1.#IND00...
18
1726
by: Daniel | last post by:
Hey guys I have an instance of an object say: List<Object> myList = new List<Object>(); Object myObject = new Object(); myObject.PositionVector = new Vector3(10,10,10); ...
4
4364
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state...
0
1026
by: ppuniversal | last post by:
hello, I am making an application where I have to copy the values of some of the tuples(which are not already copied into the files) from my database in MySQL into two files.Now I have an attribute...
4
1864
by: sam | last post by:
hI, I am little confused here See i have int wordlen=10; when int s is array s++; whats the meaning of this
3
4191
by: azegurb | last post by:
hi I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased. but i would like how create SUM function that automatically sums each added row...
6
2879
by: azegurb | last post by:
Hello, I have one question again i created one table again and in this table i added some another options for ex at the previous table there were only one problem sum of the dynamically added...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.