Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Getting value of selected Radio group

Reply
Thread Tools

Getting value of selected Radio group

 
 
Peter
Guest
Posts: n/a
 
      08-29-2003
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.

 
Reply With Quote
 
 
 
 
N Clements
Guest
Posts: n/a
 
      08-30-2003
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[i].checked == true )
val = document.myform.mygroup[i].value;
}
alert( "val = " + val );

Hope that helps.

N. Clements
Brainbench MVP for _Javascript
www.brainbench.com
http://www.velocityreviews.com/forums/(E-Mail Removed).242.mailshell.com
Remove 2nd through 4th spam to reply.



Peter <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      08-30-2003
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[i].checked == true )
> val = document.myform.mygroup[i].value;
> }
> alert( "val = " + val );
>
> Hope that helps.
>


Thanks loads - thats great!!


 
Reply With Quote
 
Steve van Dongen
Guest
Posts: n/a
 
      08-31-2003
On Sat, 30 Aug 2003 17:38:36 +0000 (UTC), Peter <(E-Mail Removed)>
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[i].checked == true )

{
>> val = document.myform.mygroup[i].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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set qry string value based on radio btn selected mikezx10@hotmail.com ASP General 9 03-22-2010 12:44 PM
reusable function to get radio button's selected value? mad.scientist.jr@gmail.com Javascript 5 04-22-2008 07:34 PM
ASP.NET Reset Radio Button in a group of Radio Buttons Scott D Johnson ASP .Net 1 09-27-2006 10:38 PM
Radio button List problem: How to find value of Radio button list's Selected Item using javascript?? Hiten ASP .Net Web Controls 1 05-26-2004 10:32 AM
reference/retrieving selected value in two radio groups eddie wang Javascript 0 12-15-2003 11:00 PM



Advertisments