Write a jQuery code to count number of rows and columns in a table.
Answer :
File Name: jQuery.html
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<title>SP Creation</title>
</head>
<body>
<div class="container">
<div class="col">
<h2>jquery</h2>
</div>
<table class="table table-bordered" id="table">
<thead>
<tr>
<th>NAME</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>Priya</td>
<td>INDAI</td>
</tr>
<tr>
<td>Snika</td>
<td>USA</td>
</tr>
<tr>
<td>Swara</td>
<td>UK</td>
</tr>
</tbody>
</table>
<p><span id="rows"></span>X<span id="cols"></span></p>
</div>
</div>
<script>
$(document).ready(function(){
$.fn.rowCount = function() {
return $('th', $(this).find('thead')).length;
};
$.fn.columnCount = function() {
return $('tr', $(this).find('tbody , thead')).length;
};
var rowctr = $('#table').rowCount();
var colctr = $('#table').columnCount();
$('#rows').text(rowctr);
$('#cols').text(colctr);
});
</script>
</body>
</html>
Write a jQuery code to count number of rows and columns in a table.
Reviewed by technical_saurabh
on
May 03, 2021
Rating:
No comments:
Post a Comment