Learn how to efficiently count HTML table rows using JavaScript with our step-by-step guide. Master the essential techniques to streamline your web development process.

Code Example:


<!DOCTYPE html>
<html>
<head>
<style>
table, td {
  border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to return the number of tr elements in the table.</p>

<table id="myData">
  <tr>
    <td>Alok</td>
    <td>340</td>
  </tr>
  <tr>
    <td>Aman</td>
    <td>220</td>
  </tr>
  <tr>
    <td>Rakesh</td>
    <td>250</td>
  </tr>
  <tr>
    <td>Rahul</td>
    <td>520</td>
  </tr>
  <tr>
    <td>Rahul</td>
    <td>520</td>
  </tr>
  <tr>
    <td>Rahul</td>
    <td>520</td>
  </tr>
  <tr>
    <td>Rahul</td>
    <td>520</td>
  </tr>
  <tr>
    <td>Rahul</td>
    <td>520</td>
  </tr>
  
</table>
<br> 

<button onclick="test()">Check</button>
<br>
<h1>Result:<span id="result"></span></h1>



</body>
<script>
function test(){

var counter = document.getElementById("myData").rows.length;

//alert(counter);
document.getElementById("result").innerHTML=counter;
}


</script>


</html>

Leave a Reply

Your email address will not be published.