Didn't read all of your code you can change the alert to this and it will
print out the value in myValue.

alert(myValue[index]); //this will print out 2, 4, or 6 depending on which
element is clicked.

On Fri, Sep 11, 2009 at 2:40 PM, Perrin Perrin <[email protected]>wrote:

> You are probably looking for something along these lines.
>
> http://mootools.net/docs/core/Native/Array#Array:each
>
> myArray.each(function(value, index, array){
>    $(value).addEvent("click",function(event) {
>          alert(value);
>    });
> });
>
> the function that you pass to each is given three params.  The value at the
> arrays current index, in this case myArray[index], the current index, and
> the array (in this case myArray).
>
> Here is most of your code with the modifications shown above.
> http://mooshell.net/hB7KQ/
>
> --Perrin
>
>
>
> On Fri, Sep 11, 2009 at 2:29 PM, batman42ca <[email protected]> wrote:
>
>>
>> I thought I read somewhere that there was a way to do this. Here's my
>> non working code:
>>
>>  var myArray = ["one","two","three"];
>>  var myValue = [2,4,6];
>>
>>  function myFunction() {
>>    var index;
>>    for (index = 0; index < myArray.length; index++) {
>>      $(myArray[index]).addEvent("click",function(event) {
>>          alert(myValue[index]);
>>      });
>>    }
>>  } // end of myFunction
>>
>>  myFunction();
>> }
>>
>> I understand why it fails. The current value of "index" (= 3 after
>> loop completion) is used when the event triggers. How do I get around
>> this? I need to use the value of "index" while the loop is running,
>> not the value of index after the loop is complete.
>>
>>
>

Reply via email to