Passing an array of functions as a parameter

javascript, jquery

Solution

As with every other question of this type, `i` keeps on changing.

Instead, try this:

for( var i=0; i<e.length; i++) {
    (function(i) {
        // your code that depends on i not changing
    })(i);
}

Problem

Here's my code: ``` function test(e, f) { for (var i = 0; i < e.length; i++) { $('#clickme').append("<button id='op" + i + "'>" + e[i] + "</button>") $('#op' + i).click(function () { f[i](); }) } } $(function postPunk() { var func1 = function () { alert('1'); } var func2 = function () { alert('2'); } var func3 = function () { alert('3'); } test(['Option1', 'Option2', 'Option3'], [func1, func2, func3]); }) ``` The click events aren't calling the functions. If I place an alert test in the click event, it fires fine. Any ideas why this isn't working? It seems to be a problem with passing the array of functions as a parameter. Is there a better way to do this?

Original source

Related problems