I've browsed the Internet looking for an answer on this but I can't find a solution. I want to count the amount of rows my HTML table has in when a link button is pressed. Can someone help me out please?
This is what I have so far:
$('#HypSelectAll').click(function () {
var count = $('#gvPerformanceResult').children().length;
for (i = 0; i < count; i++) {
alert(i);
}
});
But some reason it returns 0 even though I have about 12 rows in my table? I've checked my ID name and it's correct.
This question is related to
javascript
jquery
html-table
Try:
var count = $('#gvPerformanceResult tr').length;
Well it depends on what you have in your table.
its one of the following If you have only one table
var count = $('#gvPerformanceResult tr').length;
If you are concerned about sub tables but this wont work with tbody and thead (if you use them)
var count = $('#gvPerformanceResult>tr').length;
Where by this will work (but is quite frankly overkill.)
var count = $('#gvPerformanceResult>tbody>tr').length;
The following code assumes that your table has the ID 'MyTable'
<script language="JavaScript"> <!-- var oRows = document.getElementById('MyTable').getElementsByTagName('tr'); var iRowCount = oRows.length; alert('Your table has ' + iRowCount + ' rows.'); //--> </script>
Answer taken from : http://www.delphifaq.com/faq/f771.shtml, which is the first result on google for the query : "Get the number of rows in a HTML table" ;)
In the DOM, a tr
element is (implicitly or explicitly) a child of tbody
, thead
, or tfoot
, not a child of table
(hence the 0 you got). So a general answer is:
var count = $('#gvPerformanceResult > * > tr').length;
This includes the rows of the table but excludes rows of any inner table.
var x = document.getElementById("myTable").rows.length;
Source: Stackoverflow.com