I've been trying to make the game Battleship in JavaScript for a school assignment, and I'm stuck trying to create the opponent AI. I've created an event for when I click a cell in the grids:
function addListener(evt) {
evt.addEventListener('click', function(){
//bunch of code
});
}
Now I run this function addListener
every time I create a new cell in the grid in my nested for loop:
yourCell.setAttribute('id', evt + String(a) + String(b));
addListener(yourCell);
What I want now is for the opponent to run this click event when I've made my turn, so I wrote this just to test out the fireEvent
function:
function enemyTurn() {
document.getElementById('yourGrid00').fireEvent('onclick');
}
According to the second code example, I set the ID of the cell to 'yourGrid00'
, which I've confirmed by inspecting the html code after the JavaScript code has been run, and the function enemyTurn()
will never run before each cell has been created and assigned an ID.
What I do not understand is that I get the following error on the fireEvent
line:
Uncaught TypeError: undefined is not a function.
Does anyone know what I'm doing wrong?