How can we count number of rows in a table using jQuery

AuthorHariom Prajapati

Pubish Date19 Jun 2022

categoryJQuery

In this tutorial we will see that how to count number of rows in a table using jQuery .lenght property.

let's see the example to count number of row present in a table using jQuery.

 

Count number of row in a table

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <title>Count number of row in table using jQuery</title>
</head>

<body>
    <table id="DemoTable">
        <tr>
            <th>Roll</th>
            <th>Name</th>
        </tr>
        <tr>
            <td>1</td>
            <td>Prabhat</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Vishal</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Neha</td>
        </tr>
    </table>
    <br>
    <button type="button" class="button">Get Total no of rows</button>

    <script>
        $('.button').click(function () {
            var TotalRows = $('#DemoTable tr').length;
            alert(TotalRows);
        })
    </script>
    
</body>

</html>

 

Output

jquery

Comments 0

Leave a comment