views:

92

answers:

1

Greeting,

if I have asp.net checkboxlist control :

<asp:CheckBoxList id="list1" runat="server">
    <asp:ListItem>One</asp:ListItem>
    <asp:ListItem>Two</asp:ListItem>
    <asp:ListItem>Three</asp:ListItem>
</asp:CheckBoxList>

how to get the text for second item which is (Two) and has index 1 using jquery when it is check or by passing the index of it?

+1  A: 

The checkbox items are given IDs in the following order:

  • list1_0 => "One"
  • list1_1 => "Two"
  • list1_2 => "Three"

Prefix the ID with the IDs of any <asp:Content ...> tags that they are sitting in.

You can then reference them from jQuery easily:

if($('#list1_1').attr('checked')) {
  // the second item is ticked, do something
}

If you are unsure of the ID given to each checkbox, then do a View Source on the page to check.

You could also write a simple function to do this:

function isListItemChecked(listIndex, listID) {
  return ( $('#'+listID+'_'+listIndex).attr('checked')==true );
}
Sam Huggill