473,606 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.l ength;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)e du
Jul 23 '05 #1
2 16432
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.l ength;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(r adiogroup){
for(i=0;i<radio group.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() ,
getInputDefault Value(), 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
9807
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 update record page that reads information in the database, and presently it will not retrieve the previously selected option from the database. I have tried all sorts of if else statements, and can't get anything to work. I want the previously...
8
10791
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 ille quis mortem cum maximus ludos, vincat
1
1782
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 has 10 radio buttons of which only one can be selected at any time.
3
2350
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) framenewinstall.CircuitNumber.checked=false; switch (transport) { case 0: Helpdesk.innerText="No Info Needed";
5
3362
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 already. If it is selected, it doesn't fire. This isn't the case with a checkbox. The event fires whether the control is already checked or not. Thanks,
1
2990
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: (RADIO BUTTON) djs: (RADIO BUTTON) hfjd: Can anyone please help me.. Thanks
2
10080
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 another radio button is selected, then the 1st textbox should disappear and should display in front of the new radio button selected. Following is my code: <html> <head> <script type="text/javascript">
1
5787
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 javascript gives me three alerts for each of them. In other words all three of them have at least one element checked and so I get three alerts with the below code. var ips = document.getElementsByTagName('input'); for(i=0; i<ips.length; i++) {...
2
1153
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 an additional default hidden button, then it is only necessary to test that said button is not selected to determine that one of the others has been selected.
0
8428
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8078
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8299
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5962
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5456
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3919
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.