I’m trying to get the “click()” function to display the value of 'i' at the time I passed in the function. But its referring back to the value of 'i' after it finished. I’m drawing a blank on how to get the function to refer to the value of 'i' when I first passed the function in.
for( var i=0; i<10; i++){
var ts = $('#<span></span>').clone().click(function(){
alert(i);
});
}
NOTE:
The '#' shouldn’t be there, neither should the '.clone()'
Something like this will work:
You can give it a try here. Though, your creation is a bit off, I’m not sure why you’d want to create a new element just to clone it, and there’s an extra
#in there….I removed both of these above, but it doesn’t affect the solution of an inner function.