Jquery - selected radio button index

Hi,

I have two radio buttons, when a radio button gets clicked i want to know the selected radio button index location. The code below, for the first radio button it gives 0. For the second, it gives -1 which is wrong.


<input checked="checked" class="subTypeSelector" id="VolumeSelected" name="SubType" type="radio" value="2" />
<input class="subTypeSelector" id="DiscussOptions" name="SubType" type="radio" value="3" />

jquery


$(".subTypeSelector").click(function () {
    var $radioChecked = $(".subTypeSelector:checked");
        var $radio = $(".subTypeSelector");
        var index = $radioChecked.index($radio);
        alert(index);
});

I am following the following here:
http://stackoverflow.com/questions/2475564/jquery-radiobutton-index

With your first line, you get only the radio element that is checked. The index() function gives you the index on the set you run it on (which is $radioChecked, which only has 1 element).

I think what you want is this:


$(".subTypeSelector").click(function () {
    var $radioChecked = $(".subTypeSelector:checked");
        var $radio = $(".subTypeSelector");
        var index = $radio.index($radioChecked);
        alert(index);
});

That’s get the whole list of them in $radio, then will find the index of $radioChecked.

Thanks!