You need to move the body of the loop into a separate function that takes i as a parameter.
You can use a regular function, for example:
for(var i=0; i<10; i++) { makeCopy(i); } function makeCopy(i) { var ts = $('#<span></span>').clone().click(function(){ alert(i); }); }
You can also use the built-in method, for example: (beware of confusing syntax)
for(var i=0; i<10; i++) { (function(i) {
source share