SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Learning...
    Join Date
    Jan 2003
    Posts
    778
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    <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
    Code:
    $(".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/2...iobutton-index
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    $(".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.

  3. #3
    Learning...
    Join Date
    Jan 2003
    Posts
    778
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •