views:

448

answers:

3

Greetings, how to get the selectedindex of check box from checkboxlist control using jquery?

Update:

This code is giving me selected index equal 0 please advive

<asp:CheckBoxList ID="chkListGroups" runat="server" 
                    style="position:absolute; top: 1115px; left: 745px; bottom: 371px;" 
                    DataSourceID="SqlDSGroups" DataValueField="Groups" 
                    onclick="test()">
                </asp:CheckBoxList>


....................
java script function
.....................
  function test(){
  $('#<%=chkListGroups.ClientID %>').click(function() {
                var selectedIndex = $('#<%=chkListGroups.ClientID %>').index($(this));


                alert(selectedIndex);


            });
        }
A: 

like so if you need the index of the last selected checkbox

var query = (from o in CheckBoxList1.Items.OfType<ListItem>()  
                     where o.Selected  
                     select o).Take(1).Reverse().ToList();   
VoodooChild
OMFG! All this code to get a selected index?! You should consider other programming languages, dude...
Seb
This doesn't use jQuery.
tvanfosson
How does that get you the selected index? That's just a really weird ass way to get a list containing the first selected element in it.
R0MANARMY
sh*t my mistake this will only give you the last index of the selected item....oops
VoodooChild
Don't you guess this is a big overhead ?
Rbacarin
@VoodooChild: So the question is about getting selected item client side, where as your code is server side C# code. Also, your code just returns the element, not the index of the element in the list (as the question requested). Lastly, had the question been about getting the first selected element in a list, it would have been far easier to get it using `CheckBoxList1.Items.OfType<ListItem>().FirstOrDefault( s => s.Selected )`.
R0MANARMY
+3  A: 

Use a selector for the collection, then use index with the element that you are interested in, here the one that is checked.

var checkboxes = $('input:checkbox').;
var selectedIndex = checkboxes.index(checkboxes.find(':checked'));

To get the index of a clicked checkbox use:

$('input:checkbox').click( function() {
    var selectedIndex = $('input:checkbox').index( $(this) );
    ... now do something with it...
});

EDIT: Based on your code sample:

var checkboxes = $('#<%=chkListGroups.ClientID %>').find('input:checkbox');
checkboxes.click(function() { 
    var selectedIndex = checkboxes.index($(this)); 
    alert(selectedIndex); 

});  
tvanfosson
what I want exactly is the index of the clicked checkbox
Eyla
@Eyla - I added an example of a click handler that will give you the index of the clicked checkbox.
tvanfosson
@tvanfosson I used your code to get selected index but I'm getting 0 index all the time no matter which check box I selected.any advice??
Eyla
@Eyla - which sample, the first or second? The second one will give you the one that is being clicked. The first one will give you the first checked one.
tvanfosson
@tvanfosson the secnd one is always giving me selected index =0please see updated code in the main question
Eyla
@Eyla - Nah -- that won't work. I don't remember how that control renders, but your selector is selecting not the checkboxes themselves but the container that the checkboxes are in. I'll update my answer with a possible solution.
tvanfosson
A: 
get value: $("input:checked").val();
get id: $("input:checked").attr('id');

//or this:
var counter=0;
$('input:checkbox').each(function(){
   if($(this).is(":checked"))
   {
     var Index = counter;
   }
   else{
     counter++;
   }

   //So Index is what you want.
});

I haven't tested this, but it's something like this code. I hope it helps you

Rbacarin
You do realize that the jQuery each (http://api.jquery.com/each/) function already keeps track of the index for you and passes it into your callback as an argument so you don't need to keep the count manually, right?
R0MANARMY
In fact, I didn't know. Thank you for the comment.
Rbacarin