amit
amit

Reputation: 10271

Passing value to eventListener functions

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

Upvotes: 0

Views: 419

Answers (2)

BernzSed
BernzSed

Reputation: 1139

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);
        });
}

Upvotes: 0

user35288
user35288

Reputation:

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.

Upvotes: 1

Related Questions