Getting value of selected Radio group

Discussion in 'Javascript' started by Peter, Aug 29, 2003.

  1. Peter

    Peter Guest

    Hi all,

    Sorry if this is an obvious/common question, but I'm trying to get the
    value of a radio button using JavaScript.

    Where I can easily get the value of a text field:

    document.myform.myfield.value

    how do I get the value of the *selected* radio button? E.g. given:

    <input type="radio" name="mygroup" value="1">
    <input type="radio" name="mygroup" value="2">

    using:

    document.myform.mygroup.value

    doesn't work as there are two (in this case) radio buttons with the same
    name!

    Thanks in advance!

    Peter.
     
    Peter, Aug 29, 2003
    #1
    1. Advertising

  2. Peter

    N Clements Guest

    Try using something like the following in your javascript function:


    var val = 0;

    for( i = 0; i < document.myform.mygroup.length; i++ )
    {
    if( document.myform.mygroup.checked == true )
    val = document.myform.mygroup.value;
    }
    alert( "val = " + val );

    Hope that helps.

    N. Clements
    Brainbench MVP for _Javascript
    www.brainbench.com
    .242.mailshell.com
    Remove 2nd through 4th spam to reply.



    Peter <> wrote in news:bio94b$1k9$1
    @hercules.btinternet.com:

    > Hi all,
    >
    > Sorry if this is an obvious/common question, but I'm trying to get the
    > value of a radio button using JavaScript.
    >
    > Where I can easily get the value of a text field:
    >
    > document.myform.myfield.value
    >
    > how do I get the value of the *selected* radio button? E.g. given:
    >
    > <input type="radio" name="mygroup" value="1">
    > <input type="radio" name="mygroup" value="2">
    >
    > using:
    >
    > document.myform.mygroup.value
    >
    > doesn't work as there are two (in this case) radio buttons with the

    same
    > name!
    >
    > Thanks in advance!
    >
    > Peter.
     
    N Clements, Aug 30, 2003
    #2
    1. Advertising

  3. Peter

    Peter Guest

    N Clements wrote:
    > Try using something like the following in your javascript function:
    >
    >
    > var val = 0;
    >
    > for( i = 0; i < document.myform.mygroup.length; i++ )
    > {
    > if( document.myform.mygroup.checked == true )
    > val = document.myform.mygroup.value;
    > }
    > alert( "val = " + val );
    >
    > Hope that helps.
    >


    Thanks loads - thats great!!
    :)
     
    Peter, Aug 30, 2003
    #3
  4. On Sat, 30 Aug 2003 17:38:36 +0000 (UTC), Peter <>
    wrote:

    >N Clements wrote:
    >> Try using something like the following in your javascript function:
    >>
    >>
    >> var val = 0;
    >>
    >> for( i = 0; i < document.myform.mygroup.length; i++ )
    >> {
    >> if( document.myform.mygroup.checked == true )

    {
    >> val = document.myform.mygroup.value;

    break; // stop searching as soon as one is found
    }
    >> }
    >> alert( "val = " + val );
    >>
    >> Hope that helps.
    >>

    >
    >Thanks loads - thats great!!
    >:)


    Just add the break statement like above to reduce the amount of work
    being done.

    Regards,
    Steve
     
    Steve van Dongen, Aug 31, 2003
    #4
    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. Scott D Johnson
    Replies:
    1
    Views:
    1,096
    Scott D Johnson
    Sep 27, 2006
  2. Hiten
    Replies:
    1
    Views:
    663
    Vidar Petursson
    May 26, 2004
  3. Replies:
    9
    Views:
    1,051
    MikeR
    Mar 22, 2010
  4. eddie wang
    Replies:
    0
    Views:
    112
    eddie wang
    Dec 15, 2003
  5. Replies:
    5
    Views:
    194
    Peroli
    Apr 22, 2008
Loading...

Share This Page