fairly new to HTML/Javscript here. I have an HTML page with squares in it, and I am trying to get each square to move a certain way when the user clicks on it. I have an array called board that stores the names of the squares and I am trying to add an event listener to all but one square called "red." Calling getMove() on an element causes it to move in a direction determined by the placement of other squares.
addListeners() is being called, but the event listeners are not working. Any suggestions?
function addListeners()
{
for(var i = 0; i < dim; i++)
{
for(var j = 0; j < dim; j++)
{
if(board[i][j] != "red")
{
console.log(board[i][j]);
var elem = document.getElementById(board[i][j]);
elem.addEventListener("click", function(){getMove(elem);});
}
}
}
}
document.addEventListener("DOMContentLoaded", addListeners);