jQuery: count number of rows in a table

JqueryCountRow

Jquery Problem Overview


How do I count the number of tr elements within a table using jQuery?

I know there is a similar question, but I just want the total rows.

Jquery Solutions


Solution 1 - Jquery

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!

Solution 2 - Jquery

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;

Solution 3 - Jquery

Alternatively...

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

jsFiddle DEMO

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

Solution 4 - Jquery

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.

Solution 5 - Jquery

Here's my take on it:

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

USAGE:

var rowCount = $('#productTypesTable').rowCount();

Solution 6 - Jquery

I got the following:

jQuery('#tableId').find('tr').index();

Solution 7 - Jquery

> try this one if there is tbody

Without Header

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

If there is header then

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

Enjoy!!!

Solution 8 - Jquery

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

<p id="num_results">Number of results: <span></span></p>

<div id="results"></div>

<script type="text/javascript">
$(function(){
	ajax();
})

//Function that makes Ajax call out to receive search results
var ajax = function() {
	//Setup Ajax
	$.ajax({
		url: '/path/to/url', //URL to load
		type: 'GET', //Type of Ajax call
		dataType: 'html', //Type of data to be expected on return
		success: function(data) { //Function that manipulates the returned AJAX'ed data
			$('#results').html(data); //Load the data into a HTML holder
			var $el = $('#results'); //jQuery Object that is holding the results
			setTimeout(function(){ //Custom callback function to count the number of results
				callBack($el);
			});
		}
	});
}

//Custom Callback function to return the number of results
var callBack = function(el) {
	var length = $('tr', $(el)).not('tr:first').length; //Count all TR DOM elements, except the first row (which contains the header information)
	$('#num_results span').text(length); //Write the counted results to the DOM
}
</script>

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

Solution 9 - Jquery

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;
	

Solution 10 - Jquery

row_count =  $('#my_table').find('tr').length;
column_count =  $('#my_table').find('td').length / row_count;

Solution 11 - Jquery

var trLength = jQuery('#tablebodyID >tr').length;

Solution 12 - Jquery

document.getElementById("myTable").rows.length;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanjanView Question on Stackoverflow
Solution 1 - JquerytvanfossonView Answer on Stackoverflow
Solution 2 - JqueryJames MobergView Answer on Stackoverflow
Solution 3 - JqueryDevlshOneView Answer on Stackoverflow
Solution 4 - JqueryjjromanView Answer on Stackoverflow
Solution 5 - JqueryRicky GView Answer on Stackoverflow
Solution 6 - JquerychifliiiiiView Answer on Stackoverflow
Solution 7 - JqueryBornToCodeView Answer on Stackoverflow
Solution 8 - JquerydennismonsewiczView Answer on Stackoverflow
Solution 9 - JqueryZoeView Answer on Stackoverflow
Solution 10 - JqueryCyberneticView Answer on Stackoverflow
Solution 11 - JquerysivaprakashView Answer on Stackoverflow
Solution 12 - JqueryHasan ZafariView Answer on Stackoverflow