jQuery length Count table Rows

Introduction

Use the length property to count rows in an HTML table.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Get Number of Rows in a Table</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            var rowCount = $("#myTable tr").length;
            document.getElementById("demo").innerHTML = rowCount;
        });//from w ww.j av a2s  .  c  om
    });
</script>
</head>
<body>
    <p id="demo"></p>
    <table id="myTable" border="1" width="140">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr>
            <td>1</td>
            <td>CSS</td>
            <td>25</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Java</td>
            <td>18</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Javascript</td>
            <td>14</td>
        </tr>
    </table>
  <br>
    <button type="button">Get Number of Rows</button>
</body>
</html>



PreviousNext

Related