Count table rows using jquery

This code snippet will count all the table rows using jQuery and assign it to the rows variable.

var rows = $('#myTable tr').length;

The code searches for the target table with an id of 'myTable', it will then count the total 'tr' tags found.

If you want to avoid heading rows being counted, you can precede the tr with the tbody like so:

var rows = $('#myTable tbody tr').length;

Count table rows onload

If your code is returning 0 every time, make sure you're executing the code after the page has loaded, you can achieve this with the onload function like so:

$( window ).load(function() {
     var rows = $('#myTable tbody tr').length;
});
Sign Up

NEXT: Build an HTML5 responsive contact form

In this tutorial we'll be using the latest HTML5 form attributes with modern CSS responsive techniques to create a flexible, cross-platform contact form.

comments powered by Disqus
Sign Up

Popular Tags

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.