Jahangir
Jahangir

Reputation: 71

Can I count row of a table " $("this tbody tr").length?

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);
})

Upvotes: 7

Views: 7948

Answers (1)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196187

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;
    })

Upvotes: 12

Related Questions