If you'd like to do this in a way that is similar to your current code, it's very easy. Simply call a function in your loop, pass it the i
variable, and set up the hover()
callbacks inside that function. The only change to your code is the function call and function definition:
for ( i = 1; i < 4; i++ ) {
setupHover( i );
}
function setupHover( i ) {
$( '#buttons #' + i ).hover( function() {
$( this ).text( i );
}, function() {
$( this ).text( 'Button' );
});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="buttons">
<h1 id="1">Button</h1>
<h1 id="2">Button</h1>
<h1 id="3">Button</h1>
</div>
Why does this work where the original doesn't? Note that the two hover()
callbacks are not called at the time your loop runs. They are called later, when you roll the mouse over the elements.
So the original version the for
loop has already run to completion, long before the hover()
callbacks get called in response to mouse movement. The i
variable already has its final value of 4 before those callbacks run.
However, by calling a function inside the loop and passing i
into that function, the called function captures a reference to the variable you pass in. Note that the i
inside the setupHover()
function is a separate variable from the i
in your for
loop. It's only coincidental that it has the same name, but it really is a separate variable that is unique to each invocation of the function.
Read up on "JavaScript closures" for more information.
You can also do the same thing in other ways, such as the technique in another answer where a function returns another function. This is a useful approach in some situations, but if you simply need a closure it is usually overkill.
Any function call can create a closure, so the only one you need is a simple replacement of the loop body with a function call - either a named function outside the loop as in my example, or as you noted in your comment, an anonymous function called inline in the loop. It's the act of calling the function that creates a closure, whichever way you define and call the function.