I am new to this platform and I just encountered a problem like this: I want to make a 9x9 button grid so when the user clicks onto a button with coordinate (x,y), the value of x and y is saved into two global variables. Below is my code:
However, when I test it, no matter where I click, the value of clicked_row and clicked_col will always be 9 and 9. I think it is because the function only makes the two values equal to i and j and their value stopped at 9 in the for loop. Is there a way to pass the value i and j into the function? Thanks a lot.
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
let btn = document.createElement("button");
btn.innerHTML = ("(").concat((i + 1).toString(), ", ", (j + 1).toString(), ")"); //(i, j)
btn.className = "button";
btn.onclick = function() {
clicked_row = i;
clicked_col = j;
};
document.body.appendChild(btn);
}
document.body.appendChild(document.createElement("BR"));
}