Following part of my javscript(using jquery).
list = [\'a\', \'b\', \'c\'];
for(var i = 0 ; i< list.length ; i++) {
$(\"click here\").
You'll need a closure new scope as the iteration finishes before the event handler is triggered, so when the click happens, the loop has finished and i
is the last value it's set to, a new scope keeps the value of i
local to that scope
list = ['a', 'b', 'c'];
for(var i=0; iclick here").click(function(){
foo(list[j]);
}).appendTo('#sometag');
}(i));
}
function foo(val) {
console.log(val);
}
Another option is to add more jQuery
list = ['a', 'b', 'c'];
$.each(list, function(index, item) {
$("", {text : 'click here',
on : {
click : function() {
foo(item);
}
}
}).appendTo('#sometag');
});
function foo(val) {
console.log(val);
}