I’m trying to make a battleship game, the table is made up by 10 rows and 10 columns of buttons with no unique id for each
<table id="myTable">
<tr >
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
</tr>
<tr >
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
<td><button type="button"></button></td>
</tr>
<!-- it's quite longer-->
</table>
to get the coordinates of the cell when any button is clicked, I did this
but I returns
table.addEventListener("click",function(){
let col=this.closest('td').index();
let row=col.closest('tr').index();
shot([row,col]);
});
but the console throws typeError for this keyword
Uncaught TypeError: Cannot read property ‘index’ of null