I have a situation whereby, i need to create buttons dynamically and i need to attached an onclick event to each of them. All of the above is done fine. However, when one o
You can use jQuery proxy
var buttons = $("a");
for (var i = 0; i < buttons.length; i++){
$(buttons[i]).unbind("click").click(
$.proxy(
function(){
alert(this);
return false;
},
i
)
);
}
or capture your arguments by creating new function
var buttons = $("a");
for (var i = 0; i < buttons.length; i++){
$(buttons[i]).unbind("click").click(
function(arg){
return function(){
alert(arg);
return false;
};
}(i)
);
}
Just run one of this examples in firebug console to see the effect.