SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: Persistant Array Indices
-
Jul 27, 2006, 10:04 #1
- Join Date
- Jun 2006
- Location
- New Mexico
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Persistant Array Indices
Hiya folks,
I am creating a handful of buttons on the fly using ajax... Basically, what I have are two arrays of data that I get from the server (this.values[] and this.commands[]). One array contains the buttons' values, the other contains the command strings that I will send back to the server with the buttons' onClick functions. Right now, for testing, the onclick functions are just alerting the command rather than sending it back to the server. Here is my code for this:
Code:var x; for(x in this.values) { var button = document.createElement("input"); var br = document.createElement("br"); button.type = "button"; button.value = this.values[x]; button.onclick = function(){alert(this.commands[x];}; this.cmdDiv.appendChild(button); this.cmdDiv.appendChild(br); }
Code:button.onclick = function(){alert(button.value);};
So it appears that each time the iterator, x, increments, it's reassigning the previous buttons with the new current value.. so that by the time x is finished stepping through the array, all buttons have the same values for their value property and onclick funtion (the last value in the arrays).
From a programmer's perspective, this is raunchy behavior that completely nullifies the usefullness of an array.. there must be a way around this for javascript..?? Can anyone tell me how to solve this issue?
Thanks greatly!
-JessBluesky Saddle Blankets (and other handmade wool stuff)
-
Jul 27, 2006, 13:52 #2
- Join Date
- Jan 2005
- Location
- Too far up north
- Posts
- 1,566
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try
Code:button.onclick = function(){ alert(this.value); };
-
Jul 27, 2006, 16:10 #3
- Join Date
- Jun 2006
- Location
- New Mexico
- Posts
- 35
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey!
Thanks for the quick reply. It works!Great!! (I've been banging my head on this one for a few days...) Whew!
-JessBluesky Saddle Blankets (and other handmade wool stuff)
Bookmarks