Organizational Research By

Surprising Reserch Topic

jquery count number of rows in a table


jquery count number of rows in a table  using -'jquery,count,row'

asked Sep 21, 2015 by loknath.ganji
0 votes
8 views



Related Hot Questions

8 Answers

0 votes

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!

answered Sep 21, 2015 by sameer rathore
0 votes

If you use or in your table, you'll have to use the following syntax or you'll get a incorrect value:

var rowCount = $('#myTable >tbody >tr').length;
answered Sep 21, 2015 by nikhilapatil
0 votes

Well, I get the attr rows from the table and get the length for that collection:

$("#myTable").attr('rows').length;

I think that jQuery works less.

answered Sep 21, 2015 by deepak
0 votes

Alternatively...

var rowCount = $('table#myTable tr:last').index() + 1;

jsFiddle DEMO

This will ensure that any nested table-rows are not also counted.

answered Sep 21, 2015 by okesh.badhiye
0 votes

Here's my take on it:

//Helper function that gets a count of all the rows  in a table body 
$.fn.rowCount = function() {
    return $('tr', $(this).find('tbody')).length;
};

USAGE:

var rowCount = $('#productTypesTable').rowCount();
answered Sep 21, 2015 by amit.gupta
0 votes

I needed a way to do this in an AJAX return, so I wrote this piece:

Number of results:

Obviously this is a quick example, but it may be helpful.

answered Sep 21, 2015 by sachin valanju
0 votes

I found this to work really well if you want to count rows without counting the th and any rows from tables inside of tables:

var rowCount = $("#tableData > tbody").children().length;
answered Sep 21, 2015 by akasati02
0 votes

try this one if there is tbody

Without Header

$("#myTable > tbody").children.length

If there is header then

$("#myTable > tbody").children.length -1

Enjoy!!!

answered Sep 21, 2015 by kinnari

...