I am running a for function which dynamically creates elements based on an entered number. I have an event listener for when the div is clicked.
This is the function that I am using to make these buttons.
function createGame(value) {
previousNo = value;
for (i = 0; i < value; i++) {
gameCircle = document.createElement("div");
gameCircle.className = 'gameCircle';
gameCircle.id = "circle" + i;
gameCircle.onclick = function() {
checkGame(i);
};
document.getElementById("circleGame").appendChild(gameCircle);
}
}
And this is the function I am calling.
function checkGame(j) {
var nextNo = circleArr.shift();
if (j == nextNo) {
document.getElementById("circle" + i).style.backgroundColor = "#178a00";
} else {
console.log(j);
getValueGame();
}
}
Everytime the function runs, it returns six. Additionally, checking the onclick listener assigned says it runs this function.
function () {
checkGame(i);
}
Why does the function return six on every button, and why is the i value not being assigned to the function?