I am making a table using jquery, where the ids of the tiles are generated using a nested FOR loop. I am trying to add and event listener to the tiles, using their ids, so I can run a function when clicking them. However, the id, stored in the variable boatStatusClient is not recognised, and throws an error. Can anyone see the problem?
for (y_client = 1; y_client < 11; y_client++) {
battlefield_client += "<tr>";
for (x_client = 1; x_client < 11; x_client++) {
battlefield_client += "<td onclick = '' class = 'tile' style='border: 3px solid black;' id=" + "cell_client_" + x_client + "_" + y_client + "><pre> </pre></td>";
boatStatusClient = document.getElementById('cell_client_' + x_client + "_" + y_client);
console.log(boatStatusClient);
boatStatusClient.addEventListener("click", function(){boatGrid.placeBoat_client()});
}
battlefield_client += "</tr>";
}
$(document).ready(function() {
$("#tableGrid_client").html(battlefield_client); //loads table