Monday, December 5, 2011

How do I count the number of tr elements within a table using jquery.

This can be a most basic question, but it may keep us busy us for hours wasting our time.

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!
$('#myTable tr').size() will return the same value
the docs indicate that length is preferred over size() because it's faster

No comments:

Post a Comment