user8512043
user8512043

Reputation: 1157

Get Checked True/False From GridView in jQuery

I've a GridView where I've to retrieve the checked values from the CheckBox like if in the GridView (Row-wise), the row is set to true or false, then I've to get the values like true/false from the GridView using jQuery and set it to another CheckBox that's out of the GridView. So I was trying to use the following:

$("#grdDetails tr").click(function () {
       $(this).find('td').each(function (e) {
       $("#ContentPlaceHolder1_txtUserName").val($(this).closest('tr').find("span[id*=lblName]").text());
       $("#ContentPlaceHolder1_txtEmail").val($(this).closest('tr').find("span[id*=lblEmail]").text());

         if ($(this).closest('tr').find("input[id*=chkStatus]").attr('checked') == true) //Here I am trying to check the status of the CheckBox from GridView
         {
               $('#ContentPlaceHolder1_chkStatus').attr('checked', true);
         }
         else
         {
               $('#ContentPlaceHolder1_chkStatus').attr('checked', false);
         }
    });
});

I am not sure if I am doing the right thing and trying to check status of the CheckBox from the GridView.

Note: I am writing again - The GridView has a column Status and it shows if any person or member is active with a CheckBox in a website. My requirement is when I click on a row, that should retrieve all the information from that specific row and the CheckBox status like true/false as well. Right now, I am able to retrieve other values using jQuery except the CheckBox. As an example, see the below image: In the second row, the CheckBox is unchecked that means false. In the form, it should remain unchecked as well.

Check Status

Upvotes: 1

Views: 1003

Answers (1)

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

jQuery .attr works fine when the attribute is hard-coded. For the properties / states changed on the client side there are other methods. I would rewrite the code like this.

 if ($(this).closest('tr').find("input[id*=chkStatus]").is(':checked')/* redundant == true*/)
 {
       $('#ContentPlaceHolder1_chkStatus').prop('checked', true);
 }
 else
 {
       $('#ContentPlaceHolder1_chkStatus').prop('checked', false);
 }

Upvotes: 2

Related Questions