I am trying to create functions with the same name as the buttons' ids
var a = ["ab1","aj6","yt5","gf5","js9"]
for (i = 0; i < a.length; i++) {
var el = a[i];
function el(){
list.find(el);
}
$(function() {
$("#"+el).click(el);
}
}
When I do this, I get an
Uncaught TypeError: Cannot read property 'handler' of undefined on the anonymous function
. But when I change the function name to say "calculate" rather then the var el, it works fine. Any ideas?
el. You use the same name for two different things. Also afaik, it is not clearly specified how function declarations inside blocks are handled. You should move the function declaration outside of theforloop or use a function expression. Apart form that, yes you can create functions in a loop, but there is a catch: Javascript closure inside loops - simple practical example