Passing a value to the eventListener function

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 var num

to the eventlistener thumbClick

. but I can not. if i try to display this value it gives undefined value. Search engine help

0


a source to share


2 answers


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

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

      



var num must be available for this anonymous function.

+1


a source


One way to do this is to create a dynamic function.

Something like that. (I am based on my experience with other languages ​​in ECMAScript, you may need 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);
        });
}

      

0


a source







All Articles