Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  19] [ 4]  / answers: 1 / hits: 26175  / 13 Years ago, sat, december 3, 2011, 12:00:00
<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>

More From » javascript

 Answers
43

Try this:



var rows = document.getElementById('myTableId').getElementsByTagName('tbody')[0].getElementsByTagName('tr').length;


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.


[#88772] Thursday, December 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cameron

Total Points: 591
Total Questions: 112
Total Answers: 88

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;