Q:

How to Count Number of Rows in a Table Using jQuery

0

How to Count Number of Rows in a Table Using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the length Property

You can simply use the length property to count or find the number of rows in an HTML table using jQuery. This property can be used to get the number of elements in any jQuery object.

Let's try out the following example to understand how it actually works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Get Number of Rows in a Table</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            var rowCount = $("#myTable tr").length;
            alert(rowCount); // Outputs: 4
        });
    });
</script>
</head>
<body>
    <table id="myTable">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <tr>
            <td>1</td>
            <td>John</td>
            <td>25</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Peter</td>
            <td>18</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Harry</td>
            <td>14</td>
        </tr>
    </table>
    <button type="button">Get Number of Rows</button>
</body> 
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Merge Two Arrays in JavaScript... >>
<< How to AJAX Submit a Form in jQuery...