I can't pass different values of variable through $.get() function please check this code to learn more about my problem
var addressFieldValues = ['address1', 'address2', 'address3'];
for(i=0; i<addressFieldValues.length; i++) {
var address = addressFieldValues[i];
$.get('function.php', address, function(data){
alert(address); // alerts address1 all time
});
}
Why is it alerting "address1" these 3 times? since it should alert 3 different addresses at all.
let addressin support browsers or a closure function. The reason:var addressonly exists once for the entire loop and, because$.get()behaves asynchronously,alert(address);isn't evaluated until after theforloop is done and has reassignedvar addressmultiple times..forEach()instead of a loop would solve the problem too.