for ( var i = 0; i < 8; i++ ) {
$( "#image" + i ).click( function() {
console.log( "test: " + i );
} );
}
I wait for test: 0, test: 1, test: 2... here in the console when I click my image but there are only: "test: 8" messages. Why does it work like this? How to save current value of i-variable? I need it to do something like this:
for ( var i = 0; i < 8; i++ ) {
$( "#image" + i ).click( function() {
$( "#anotherImage" + i ).css( "opacity", "0.5" );
} );
}
I have 8 anotherImages with IDs: anotherImage0, anotherImage1, ..., anotherImage7 :)