Read the cellIndex
property of the clicked cell and rowIndex
of its parent TR
node. This gives you coordinates of the cell:
coords = function(td) {
return [td.cellIndex, td.parentNode.rowIndex];
}
Create an array of adjacent rows and columns:
var adj = [
[x - 1, y - 1],
[x - 0, y - 1],
[x + 1, y - 1],
[x + 1, y - 0],
[x - 1, y - 0],
[x - 1, y + 1],
[x - 0, y + 1],
[x + 1, y + 1]
];
Iterate over all cells in the table and mark cells whose coordinates are in the array:
var tds = game.getElementsByTagName("TD");
[].forEach.call(tds, function(td) {
if(contains(adj, coords(td)))
td.className = "hot";
else
td.className = "";
});
Complete working example: http://jsfiddle.net/FByXq/