I have 3 tables with same class name table-sort
. I would like to access those table by .each()
and count the tr
inside the tbody
.
is it $("this tbody tr").length
?
$('.table-sort').each(function(index) {
var rowCount = $("this tbody tr").length; //not work , Could you please correct this?
var rowCount1 = $(this).find('tbody > tr').length; //this is working fine
alert(rowCount + '-' + rowCount1);
})
Answer: Use the length Property You can simply use the length property to count or find the number of rows in an HTML table using jQuery. This property can be used to get the number of elements in any jQuery object.
To count the number of rows, the “#Table_Id tr” selector is used. It selects all the <tr> elements in the table. This includes the row that contains the heading of the table. The length property is used on the selected elements to get the number of rows.
Right-click on the selected table and select Table Properties from the shortcut menu. Click on the Row tab — the number of rows selected is listed at the top of the dialog box.
Here is the code
$('.table-sort').each(function(index) {
var rowCount = $("tbody tr", this).length; //will work now..
var rowCount1 = $(this).find('tbody > tr').length; //this is working fine
alert(rowCount + '-' + rowCount1);
})
But the second code you use, which works, should be enough ..
You could also use the inherent table properties of the table DOM object
$('.table-sort').each(function(index) {
var rowCount = this.rows.length;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With