I would like to know how to click on a button in an HTML table and get the row and column number returned to me: For example, with the following table:
Try this:
function getId(element) {
alert("row" + element.parentNode.parentNode.rowIndex +
" - column" + element.parentNode.cellIndex);
}
<table>
<tr>
<td><input type="button" value="button" onclick="getId(this)"></td>
<td><input type="button" value="button" onclick="getId(this)"></td>
<td><input type="button" value="button" onclick="getId(this)"></td>
</tr>
<tr>
<td><input type="button" value="button" onclick="getId(this)"></td>
<td><input type="button" value="button" onclick="getId(this)"></td>
<td><input type="button" value="button" onclick="getId(this)"></td>
</tr>
<tr>
<td><input type="button" value="button" onclick="getId(this)"></td>
<td><input type="button" value="button" onclick="getId(this)"></td>
<td><input type="button" value="button" onclick="getId(this)"></td>
</tr>
</table>
Most generic version of @Gremash js function
function getId(element) {
alert("row" + element.closest('tr').rowIndex +
" -column" + element.closest('td').cellIndex);
}