473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Getting value of selected Radio group

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.

Jul 20 '05 #1
3 110135
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
no****@spam.spam.spam.242.mailshell.com
Remove 2nd through 4th spam to reply.

Peter <no****@nospam.com> 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.

Jul 20 '05 #2
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!!
:)

Jul 20 '05 #3
On Sat, 30 Aug 2003 17:38:36 +0000 (UTC), Peter <no****@nospam.com>
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
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gmanon | last post by:
Hi, this is a newbie question. How can I catch the selected radio value from a form that is feed by the mysql database? EX.: <input type="radio" name="myname" value="<?php $myvalue ?>"> ...
1
by: Verner Vaz | last post by:
Select Email ID based on 'a' selected radio button Hello there, I have a simple form with some fields to be filled in and when submitted is emailed to a couple of email ID. Now, this form...
6
by: Craig Keightley | last post by:
I have a page that has n number of radio groups (yes/No) how can i prevent the form being submitted if more than one radio group is not selected? By default all radio groups are unchecked ...
2
by: Mark Haase | last post by:
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...
1
by: Rage Matrix | last post by:
Hi all, I have a small problem with Access radio button groups in VBA. I've got a radio button group called fraSearchMode with three radio buttons in it. At a certain point, I want to see which...
8
by: Zlatko Matić | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
2
by: cindy | last post by:
When my datalist loads I am trying to make the checked value in a radio button group reflect which value is stored in the data and then fire and event when the CheckChanged event fires. private...
1
by: querry | last post by:
`Hi all, I am trying to get the selected radio button item in a radio button list using javascript. I am successful at doing this, but the problem is I have several Radio Button Lists, and the...
1
by: Max | last post by:
Hello group, I use this function to find the value of a radio group: function valButton(btn) { var cnt = -1; for (var i=btn.length-1; i -1; i--) { if (btn.checked) {cnt = i; i = -1;} } if...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.