In case of:
var allBusiness = ["one", "two", "three", "four"]
for (var i = 0; i < allBusiness.length; i++) {
document.getElementById(allBusiness[i]).addEventListener("click", function () {
console.log(allBusiness);
});
}
This one logs ["one", "two", "three", "four"]
However the following one where I try to log each value separately shows undefined
:
var allBusiness = ["one", "two", "three", "four"]
for (var i = 0; i < allBusiness.length; i++) {
document.getElementById(allBusiness[i]).addEventListener("click", function () {
console.log(allBusiness[i]);
});
}
why doesn't what log the corresponding names? and how to achieve that?