JS variable scope missunderstanding

Posted by meo on Stack Overflow See other posts from Stack Overflow or by meo
Published on 2010-04-22T07:55:23Z Indexed on 2010/04/22 8:13 UTC
Read the original article Hit count: 175

Filed under:

I have a little problem: slideHelpers.total = 4

for (i=1;i <= slideHelpers.total; i++) {
    $('<a href="#">' + i + '</a>').bind('click', function(){ alert('go to the ' + i + ' slide')}).appendTo('.slideaccess')
}

the alert gives out 5 what is logic, because when the function click triggers i is actually 5. But i would like to have the same i as in my <a> tag. What is the best way to handle this?

I could put i in the data() of the <a> tag for example but i am sure there is a easier way.

© Stack Overflow or respective owner

Related posts about JavaScript