<head>
<script language="javascript">
// must have the onload handler
onload = function countRows(){
var rows = document.getElementById('myTableId').getElementsByTagName('tbody')[0].rows.length;
alert( rows);
// outputs 3
}
</script>
</head>
<body>
<table id="myTableId">
<tbody>
<tr><td></td><td><input onclick="doA_Function_That_Includes_CountRows()" />
</td></tr>
<tr><td></td><td></td></tr>
<tr><td></td><td></td></tr>
</tbody>
</table>
</body>
</html>
<head> <script language=javascript> // must have the onload handler onload = function countRows(){ var
Share
Try this:
http://jsfiddle.net/y8URn/
It will count the number of
<tr>s in the<tbody>, which in turn will be the number of rows in the table.Do note that it will NOT count all of the rows in the table only in the table body. If you have a
<thead>or a<tfoot>or even a row outside of the tbody, it will not be counted.