Niranjan Godbole Niranjan Godbole - 4 years ago 160
Javascript Question

how to loop through gridview using jquery and check if any checkbox is checked

I am developing jquery based application. I have one asp.net gridview with checkboxes in every row. I want to check if any of the checkboxes checked or not. I am looping through gridview rows and checking any checkbox is checked or not as below.

var ResultArrayFirst = [];
$('#<%= gdvRegretletter.ClientID %> input[type="hidden"]').each(function () {
if ($(this).closest('tr').find('input[type="checkbox"]').prop('checked', true))
{
ResultArrayFirst.push($(this).val());
}

});
alert(ResultArrayFirst);


My above code does not works. As soon as above code is executed, all checkboxes will check. I am not sure what i Am missing here. Any help would be appreciated. Thank you.

Answer Source

To check if a checkbox is checked, use this:

if (checkbox.is(':checked')) {
   // Do your stuff
}

So, change your code like this:

if ($(this).closest('tr').find('input[type="checkbox"]').is(':checked'))
{
   ResultArrayFirst.push($(this).val());
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download