Skip to content
the poor coder

count selected gridview rows in javascript

Poor Man
Published
Reading time
2 min read
On this page

Count Selected Gridview Rows in Javascript#

Have you ever encountered a situation where you need to count the number of selected rows in a gridview using Javascript? If yes, then you are in the right place. In this post, I will share with you how to count selected gridview rows in Javascript.

Method 1: Using a Loop#

The first method to count selected gridview rows in Javascript is to loop through all the rows and check if they are selected or not. Here is the code:

Text

var gridview = document.getElementById("myGridview");
var count = 0;

for (var i = 0; i < gridview.rows.length; i++) {
    if (gridview.rows[i].cells[0].getElementsByTagName("input")[0].checked) {
        count++;
    }
}

console.log("Number of selected rows: " + count);
  • var gridview = document.getElementById("myGridview");: Gets the gridview element by its ID.
  • var count = 0;: Initializes a variable to store the count of selected rows.
  • for (var i = 0; i < gridview.rows.length; i++) { ... }: Loops through all the rows of the gridview.
  • if (gridview.rows[i].cells[0].getElementsByTagName("input")[0].checked) { ... }: Checks if the checkbox of the current row is checked or not.
  • count++;: Increments the count if the checkbox is checked.
  • console.log("Number of selected rows: " + count);: Outputs the count of selected rows to the console.

Method 2: Using jQuery#

If you are using jQuery in your project, then you can use the following code to count selected gridview rows:

Text

var count = $("#myGridview input[type='checkbox']:checked").length;

console.log("Number of selected rows: " + count);
  • $("#myGridview input[type='checkbox']:checked"): Gets all the checked checkboxes inside the gridview using the jQuery selector.
  • .length: Gets the number of selected checkboxes.
  • console.log("Number of selected rows: " + count);: Outputs the count of selected rows to the console.

These are the two methods to count selected gridview rows in Javascript. You can choose any method that suits your project requirements. I hope this post was helpful to you. If you have any questions or suggestions, feel free to leave a comment below.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC