table.rows.length in Java script returns the count of rows in the table including rows in <thead> tag. What I need is, if there is a header row (inside <thead> ) with only <th> elements, I want to neglet that row and I need only the count of rows having data in it not the header elements.
Note : I can not use table.rows.length - 1 as I can not modify Javascript. I can only modifiy in HTML side. Here is the code sample snippet. Here I need count as 4 not 5.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample</title>
<script type="text/javascript">
function call(){
alert(document.getElementById('tab').rows.length);
}
</script>
</head>
<body onload="call()">
<table id="tab" border="1">
<thead>
<th>
header 1
</th>
<th>
header 2
</th>
</thead>
<tbody>
<tr>
<td> data1</td>
<td> data1</td>
</tr>
<tr>
<td> data2</td>
<td> data2</td>
</tr>
<tr>
<td> data3</td>
<td> data3</td>
</tr>
<tr>
<td> data</td>
<td> data4</td>
</tr>
</tbody>
</table>
</body>
</html>
Reassign
idto the<tbody>element:As this element is
<HTMLTableSectionElement>one, it hasrows(HTMLCollection) property too, so you won’t have to make any changes in your JS at all.