I create some image elements with JS, and I want that when I click in each image have a different number associated. But the problem is addEventListener just take the last number of the FOR. Some help pls.
For example, when I click on the second image the number that appear on the alert is 6.
body = document.body;
test = 1;
for (i = 0; i < 6; i++) {
img = document.createElement('img');
img.src = 'https://www.w3schools.com/images/w3lynx_200.png';
img.addEventListener("click", function() {
alert(test);
});
body.appendChild(img);
test = test + 1;
}