tags:

views:

158

answers:

2
for ( var i=0; i<thumbs.length; i++)
     {
      var num = i;
      Core.addEventListener(thumbs[i], "click",  Slide.thumbClick);
     }

in the above code, i want to pass the value of var num to the thumbClick eventlistener. but i am unable to. if i try to display that value, it gives an undefined value. pls help

+1  A: 

Don't remember for sure, but you should be able to do something like this:

Core.addEventListener(thumbs[i], "click", function() {
    //...do stuff here
});

var num should still be available to this anonymous function.

W_P
Very good hint. When clicked, the 'this' becomes thumb[i]. Slide.thumbclick probably assumes another this.
xtofl
A: 

One way to do it would be to create a dynamic function.

Something like this. (I'm basing this on my experience with other ECMAScript-based languages, you might want to double-check to make sure this works.)

for ( var i=0; i<thumbs.length; i++)
{
        var num = i;
        Core.addEventListener(thumbs[i], "click",  new function(evt){
            Slide.thumbClick(num);
        });
}
BernzSed