I have an HTML table that is defined as follows:
<table id="myTable" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Smith</td>
<td>03-11-1980</td>
</tr>
</tbody>
</table>
I want to understand how to determine how many rows are in the tbody
portion of my table, instead of the table in general. How do I use JQuery to determine how many rows are in the tbody portion of a table?
Thank you!
Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).
columnCount = function() { return $('th', $(this). find('tbody')). length; }; var rowctr = $('#table1'). rowCount(); var colctr = $('#table1').
jQuery(". dname"). find("tr td:eq(1)"). val();
Like this:
$("#myTable > tbody > tr").length
The a > b
selector selects all b
elements that are direct children of an a
element. Therefore, this selector will not match rows in nested tables.
Have you tried...
$("#myTable tbody tr").length
UPDATE: as mentioned in the comment - the above example doesn't account for nested tables. If you want to cover that as well, you'd want to use the parent/child selectors instead.
$("#myTable > tbody > tr").length
var amountOfRows = $("#myTable tbody tr").length;
alert(amountOfRows);
The variable amountOfRows will hold the number of rows. You can also use $("#myTable tbody tr").size But that is a shortcut for .length
If used on nested tables see SLaks answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With