jQuery: count number of rows in a table
Use a selector that will select all the rows and take the length.
var rowCount = $('#myTable tr').length;
Note: this approach also counts all trs of every nested table!
If you use <tbody>
or <tfoot>
in your table, you'll have to use the following syntax or you'll get a incorrect value:
var rowCount = $('#myTable >tbody >tr').length;
Alternatively...
var rowCount = $('table#myTable tr:last').index() + 1;
jsFiddle DEMO
This will ensure that any nested table-rows are not also counted.