Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

each

this

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);
})
like image 276
Jahangir Avatar asked Dec 23 '10 00:12

Jahangir


People also ask

How can I get table Tbody row count in jQuery?

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.

How do I count rows in a table?

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.

How do I count rows in a Word table?

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.


1 Answers

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;
    })
like image 168
Gabriele Petrioli Avatar answered Sep 21 '22 20:09

Gabriele Petrioli