Thursday, December 17, 2009

jQuery check/uncheck all Checkboxes in ASP.NET

For some odd reason, ASP.NET GridView wraps html elements in a span element. When you apply a CssClass to the asp:CheckBox for instance, you will have a span with that class and inside you'll have an input with type=checkbox.
So how do you access all of the checkboxes?

   1:              $('.chkAllSelector > input:checkbox').click(function() {
   2:                  $('.chkRowSelector > input:checkbox').each(
   3:                      function(){
   4:                          this.checked = $('.chkAllSelector > input:checkbox').attr('checked');
   5:                      }
   6:                  );
   7:              });
   8:              $('.chkRowSelector > input:checkbox').click(function() {
   9:              if ($('.chkAllSelector > input:checkbox').is(':checked') && !($(this).is(':checked')))
  10:                  { $('.chkAllSelector > input:checkbox').removeAttr('checked'); }
  11:              });

No comments:

Archive