In following code I try to create several handler functions that must invoke different functions stored in an array of functions ('buttonHandlers'). This array is part of the outer scope:
buttonJson = {};
for (i = 0; i < buttonNames.length; i++) {
customHandler = buttonHandlers[i];
buttonJson[buttonNames[i]] = function() {
customHandler.apply();
$('#msg-dialog-confirm').dialog("close");
$('body').remove('#msg-dialog-confirm');
...
};
}
The code above results in handler functions that invoke the very last array element of the functions array ('buttonHandlers'). I want each handler function to invoke only the associated function specified by the concerning array index. How can I achieve this?