Check / Uncheck All checkboxes in Listview / Gridview with checkbox on header using JQuery.

 

I was trying to write a code to Check / Uncheck All checkboxes in Listview with checkbox on header using JQuery.

image

This is the first version of my code.

 

$("#<%=listviewName.ClientID %> 
        table tbody tr th:last input:checkbox").live('click', function () { 
   var checkedStatus = this.checked; 
    $("#<%=listviewName.ClientID %>        
       table tbody tr td:last-child input:checkbox").each(function () { 
                   this.checked = checkedStatus; 
             }); 
  });

This is the second version of my code.

 

$(document).ready(function () {  $('#<%=listviewName.ClientID%>     
    input[id*="chkselectAll"]:checkbox').live('click', function () {         
    $('#<%=listviewName.ClientID%>          
          input[id*="chkselect"]:checkbox').attr('checked', this.checked);           
        });      
  });

 

I finally end up with this version.

 

$("[id$='chkSelectAll']").live('click', function () {
    $("[id$='chkSelect']").attr('checked', this.checked);
 });

 

Note: just make sure to set clientmode to Static in the checkbox.

<asp:CheckBox ID="chkSelectAll" ClientIDMode="Static" 
      EnableViewState="false" runat="server" />

 

Just wondering how many lines of code needed to write the same in JavaScript? By the way, Any one got better version?

Advertisements
Tagged , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: