What is jQuery?

 

  • jQuery is a small, light-weight and fast JavaScript library. It’s cross-platform and supports different types of browsers. It’s also referred as Write less do more? Because it takes a lot of common tasks that needs many lines of JavaScript code to accomplish, and binds them into methods which will be called with a single line of code whenever needed.
  • It’s also very useful to simplify a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.

How to Count Number of Rows in a Table Using jQuery

Answer: Use the length Property

  • You can simply use the length property to count or find the amount of rows in an HTML table using jQuery. This property is used to get the amount of elements in any jQuery object.
  • Let’s try the following example to know how it actually works:

Example

<!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" border="1" width="140">
<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>
<br>
<button type="button">Get Number of Rows</button>
</body>
</html>

Output

Categorized in: