Finding the selected radio button?

Discussion in 'Javascript' started by Mark Haase, Jun 17, 2005.

  1. Mark Haase

    Mark Haase Guest

    Hey All--

    I'm rusty on my JavaScript but I'm pulling it back out to do a PHP/MySQL
    database application. I'm doing all the validation in PHP, but I'm using
    JavaScript to show/hide questions which depend on the responses to other
    questions, e.g. "If you answered yes to the previous question, then what
    time of day will you need your service activated?"

    One aspect of this is needing to be able to decide which radio button in
    certain groups is selected. Most of these are "yes/no", some have three
    options. Apparently the only way to do this is with a for loop? I found
    the following code many times on Google while looking for an answer:

    ================
    <form name="test">
    <input type="radio" name="myradio" />
    <input type="radio" name="myradio" />
    <input type="radio" name="myradio" />
    </form>

    <script type="text/javascript">
    //a variable that will hold the index number of the selected radio button
    for (i=0;i<document.test.myradio.length;i++){
    if (document.test.myradio.checked==true)
    theone=i
    }
    </script>
    ================

    Is this really how everybody does it? It seems like such an obvious
    shortcoming in JavaScript that I can't believe its true, but none of the
    JS references that I've found online indicate the existence of a
    selectedIndex() function or similar for a radio group.

    Thanks

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
     
    Mark Haase, Jun 17, 2005
    #1
    1. Advertising

  2. Mark Haase

    Mick White Guest

    Mark Haase wrote:

    > Hey All--
    >
    > I'm rusty on my JavaScript but I'm pulling it back out to do a PHP/MySQL
    > database application. I'm doing all the validation in PHP, but I'm using
    > JavaScript to show/hide questions which depend on the responses to other
    > questions, e.g. "If you answered yes to the previous question, then what
    > time of day will you need your service activated?"
    >
    > One aspect of this is needing to be able to decide which radio button in
    > certain groups is selected. Most of these are "yes/no", some have three
    > options. Apparently the only way to do this is with a for loop? I found
    > the following code many times on Google while looking for an answer:
    >
    > ================
    > <form name="test">
    > <input type="radio" name="myradio" />
    > <input type="radio" name="myradio" />
    > <input type="radio" name="myradio" />
    > </form>
    >
    > <script type="text/javascript">
    > //a variable that will hold the index number of the selected radio button
    > for (i=0;i<document.test.myradio.length;i++){
    > if (document.test.myradio.checked==true)
    > theone=i
    > }
    > </script>
    > ================
    >
    > Is this really how everybody does it? It seems like such an obvious
    > shortcoming in JavaScript that I can't believe its true, but none of the
    > JS references that I've found online indicate the existence of a
    > selectedIndex() function or similar for a radio group.


    Build your own:
    function selectedRadio(radiogroup){
    for(i=0;i<radiogroup.length;i++){
    if(radiogroup.checked) return i;
    }
    }

    Mick
     
    Mick White, Jun 18, 2005
    #2
    1. Advertising

  3. Mark Haase

    Matt Kruse Guest

    Mark Haase wrote:
    > One aspect of this is needing to be able to decide which radio button
    > in certain groups is selected. Most of these are "yes/no", some have
    > three options. Apparently the only way to do this is with a for loop?
    > Is this really how everybody does it? It seems like such an obvious
    > shortcoming in JavaScript that I can't believe its true, but none of
    > the JS references that I've found online indicate the existence of a
    > selectedIndex() function or similar for a radio group.


    It does seem like a bother that form inputs are handled how they are in
    javascript. But, this trouble is easily avoided by writing generalized
    functions once and then never worrying about the particulars of getting
    input values again.

    I have general functions like getInputValue(), setInputValue(),
    getInputDefaultValue(), etc at:
    http://www.javascripttoolbox.com/validations/

    They work regardless of input type, how many inputs there are with the same
    name, etc. Very handy, and especially useful when an input on your form
    changes from one type to another (radio to select, for example). By using
    generalized functions, your javascript would most likely not need to be
    changed.

    Hope that helps,

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Jun 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samy
    Replies:
    5
    Views:
    703
  2. Scott D Johnson
    Replies:
    1
    Views:
    1,082
    Scott D Johnson
    Sep 27, 2006
  3. Hiten
    Replies:
    1
    Views:
    643
    Vidar Petursson
    May 26, 2004
  4. Nige
    Replies:
    8
    Views:
    417
    Brian
    Dec 11, 2003
  5. Verner Vaz
    Replies:
    1
    Views:
    97
    David Dorward
    Oct 17, 2004
Loading...

Share This Page