Variable inside of a function?

There is such a function. Tracks click the button and creates a popup with the necessary information.
How to fix that would be for map.appendChild(createCard(i)); was available the number of the pressed button?
Now there is a bug, as i not available for some reason. If instead i substitute a number, then everything is OK.

clickPin function() {
 var pinButton = document.querySelectorAll(".map__pin");
 for (var i = 1; i < pinButton.length; i++) {
 pinButton[i].addEventListener("click", function() {
map.appendChild(createCard(i));
 var popup = document.querySelector(".map__card.popup");
popup.classList.remove("hidden");
});
}
}
April 7th 20 at 10:45
1 answer
April 7th 20 at 10:47
Solution
To snap to or instead of the var - let

let makes at each iteration to the variable i with its value (conventionally 10 variables with different value)

var makes one variable and updates (after the cycle will be available the last value)
Thank you! - rhianna commented on April 7th 20 at 10:50

Find more questions by tags JavaScript