p.campbell
p.campbell

Reputation: 100557

jQuery: check all checkboxes

Consider this scenario:

The markup:

    <asp:CheckBoxList runat="server" ID="chkSubscriptionType" 
        DataSourceID="myDS"
        CssClass="boxes" DataTextField="Name" DataValueField="Name"  />

renders to:

<input id="ctl00_cphContent_chkSubscriptionType_0" type="checkbox" name="ctl00$cphContent$chkSubscriptionType$0" />

Question: how can you use jQuery to check all boxes in this asp:CheckBoxList on document.ready? I see samples everywhere, but naming convention used by the master page throws off the samples in other places.

Upvotes: 0

Views: 1298

Answers (3)

Warren Krewenki
Warren Krewenki

Reputation: 3583

Regardless of asp/php/ruby, etc, you should be able to do something like:

$(document).ready(function(){ 
      $("input[type=checkbox]").attr("checked", "checked");
});

Upvotes: 3

Sampson
Sampson

Reputation: 268324

ASP.NET's naming conventions are slightly frustrating. You can side-step them by wrapping your element in a span, and giving it a class. You can then use that span.class to focus your selector:

$(function(){
  // On dom-ready, all checkboxes in span.names will be checked
  $("span.names :checkbox").attr("checked", "checked");
});

<span class="names">
  <asp:CheckBoxList ... />
</span>

Upvotes: 4

womp
womp

Reputation: 116977

Because of the naming container, the ID's will be auto-generated and messed up, as you mention. You can use an attribute filter to select all elements that contain the part that you do know, that is, "chkSubscriptionType". For example:

$("input[id*=chkSubscriptionType]").attr("checked", "checked");

The *= means "contains".

Upvotes: 6

Related Questions