I\'m looking for a good way to get cells X-Y position in a table. (do not confuse it with css-position, I am looking for X and Y coordinate in Cartesian coordinate system).<
window.onload = function () {
document.getElementsByTagName('table')[0].addEventListener('click', function(e) {
alert("My position in table is: " + e.target.parentElement.rowIndex + "x " + e.target.cellIndex + "y");
}, false);
}
tr, td {
padding: 0.6rem;
border: 1px solid black
}
table:hover {
cursor: pointer;
}
<table id="grid">
<tr>
<td>0, 0</td>
<td>0, 1</td>
<td>0, 2</td>
</tr>
<tr>
<td>1, 0</td>
<td>1, 1</td>
<td>1, 2</td>
</tr>
</table>
DOM Level 2 HTML cellIndex:
alert('My position in table is: '+this.cellIndex+'x'+this.parentNode.rowIndex);