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:
<table>
<tr>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
</tr>
<tr>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
</tr>
<tr>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
</tr>
</table>
How would I use JavaScript to click on the first button in the second row and have it tell me that I clicked on the first cell in the second row? Does each button need to have a unique id, or not?
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:
<table>
<tr>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
</tr>
<tr>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
</tr>
<tr>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
<td><input type="button" value="button"></td>
</tr>
</table>
How would I use JavaScript to click on the first button in the second row and have it tell me that I clicked on the first cell in the second row? Does each button need to have a unique id, or not?
Share Improve this question edited Mar 25, 2018 at 9:25 Brian Tompsett - 汤莱恩 5,87572 gold badges61 silver badges133 bronze badges asked Mar 13, 2015 at 21:20 scatterbrain29scatterbrain29 2611 gold badge3 silver badges7 bronze badges 1 |3 Answers
Reset to default 46Try 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);
}
Try this code: alert(document.getElementById("yourTableId").childNodes[1].childElementCount);
rowIndex
. and cells havecellIndex
. You can use them like so ... – Teemu Commented Mar 13, 2015 at 21:50