473,385 Members | 1,757 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,385 software developers and data experts.

Finding the selected radio button?

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[i].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
Jul 23 '05 #1
2 16415
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[i].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[i].checked) return i;
}
}

Mick

Jul 23 '05 #2
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
Jul 23 '05 #3

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

Similar topics

2
by: entoone | last post by:
I am able to make a selection of information when entering a record, with radio buttons giving the option of yes, or no. Which stores their answer as yes, or no in the database. I then have an...
8
by: Nige | last post by:
Is it possible to create an alert box when a radio button is selected? I have a group of three, and I want a different alert for each one. -- Nige Please replace YYYY with the current year...
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...
3
by: ewitkop90 | last post by:
Here is my code: <SCRIPT> function transportchange(transport) { if (framenewinstall.Helpdesk.checked) framenewinstall.Helpdesk.checked=false; if (framenewinstall.CircuitNumber.checked)...
5
by: tshad | last post by:
Is there a way to allow a user to press a radio button if it is already selected? There is an onCheckedChanged event that fires when the person presses the button and it is isn't selected...
1
by: jw01 | last post by:
Im trying to write a java script in HTML in which when a particular radio button is selected, a textbox would appear write next to the selected item. e.g (RADIO BUTTON) fhd: (RADIO BUTTON) fdj:...
2
by: jw01 | last post by:
I changed my code...Now everytime i click a radio button, a text box appears...What i want is when i hit a particular radio button, the textbox should only appear next to that radio button. And if...
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...
2
by: Dr J R Stockton | last post by:
In comp.lang.javascript message <d4lazy2m.fsf@hotpop.com>, Fri, 18 Jul 2008 23:23:13, Lasse Reichstein Holst Nielsen <lrn@hotpop.composted: Not necessarily. If each set of radio buttons has...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.