<table id="table">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
.color1 {
background: red;
}
td {
width: 50px;
height: 50px;
border: 1px solid black;
}
let trs = document.querySelectorAll("tr");
let rowNum = 1;
for (let tr of trs){
let tds = tr.querySelectorAll("td");
let col = 1;
for(let td of tds){
td.dataset.col = col++;
td.dataset.row = rowNum;
let rowCount = document.querySelectorAll("td[data-row]").length;
if (rowCount % 3 == 0){
rowNum++;
}
td.addEventListener("click", function func(){
let thisCol = td.dataset.col;
let thisRow = td.dataset.row;
let BoxCol = document.querySelectorAll('td[data-col="' + thisCol +'"]');
for (let box of BoxCol){
if (box.dataset.row == Number(thisRow) + 1){
console.log(box);
box.removeEventListener('click', func);
}
}
td.classList.add('color1');
});
}
}