SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast J.Ryan's Avatar
    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);
    }
    The result is that I get three different buttons, each showing a different (and correct) value. However, when I click on them, they all display the same command, the last one in the commands array. I then did a slight modification:
    Code:
    button.onclick = function(){alert(button.value);};
    and discovered that this new onclick function also alerted the same value for each button, again the last entry in the array of values. Even though the webpage is showing the buttons with unique values...

    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!
    -Jess
    Bluesky Saddle Blankets (and other handmade wool stuff)

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    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); };

  3. #3
    SitePoint Enthusiast J.Ryan's Avatar
    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!

    -Jess
    Bluesky Saddle Blankets (and other handmade wool stuff)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •