I want the onclick function to remember the value of the variable i and j in the for loop at the instance the onclick function is called. However the variables update before they are stored in the onclick function.
I am making a Tic Tac Toe application and don't want to manually add all the onclick statements as that would be a bad practice for larger projects.
for (j = 0; j < Columns; j++) {
var col = document.createElement("td")
row.appendChild(col)
col.id = i + "" + j;
col.onclick = function() {
console.log(i, j)
place(i + "" + j)
}
}
There are no error messages but I don't know how to save the variables the instance the onclick function is called. How can I save the variables when the onclick function is called?