...

View Full Version : How do I determine which button has been selected in a set of radio buttons?



mda2711
10-18-2003, 07:53 PM
In this code:

document.form.radio[?].value

What can I put in place of the question mark to tell me which button the user has selected?

mda2711
10-18-2003, 08:25 PM
Sorry can i rephrase the question more clearly.

I have a basic script that, when someone clicks a radio button, displays something in a text form input.
I have:

document.form_3.radfv.value=document.form_3.radfs[].value
form_3 is the name of the form i'm using, radfv is the name of the input where I want to display the infomation, and radfs is the name of the set of radio buttons. How do determine which button the user has selected instead of putting in a absolute value, ie
radfs[something]
instead of

rdfs[0]

xupaosso
10-19-2003, 05:38 AM
I just had to struggle with this myself, so I'm glad to know I'm not the only one learning this stuff! This is only my solution to this though and there may be an easier way.

Because radio buttons with the same name are treated as an array, I had to create a function that would check each one to see which one was checked and then return that value.

Ex.
for (i=0; i<document.form_3.radfs.length;i++){
if (document.form_3.radfs[i].checked==true){
valuedesired=document.form_3.radfs[i].value;}
}

"valuedesired" would then contain the selected radio button value, so then you could:

document.form_3.radfv.value=valuedesired;

Unless the output is going to something besides an element that will display text.

Hope this helps...
Does anyone else have another solution? I would like to know of a better way to do this myself!

mda2711
10-19-2003, 10:39 AM
Thanks for the help, that will be really useful

Willy Duitt
10-19-2003, 11:46 PM
Try this one. :D


<head>
<script type="text/javascript">
function validateRadio(form1) {
thisOne = -1;

for (i=0; i<form1.radio1.length; i++) {
if (form1.radio1[i].checked) {
thisOne = i;
whichOne = (thisOne+1);
}
}
if (thisOne == -1) {
alert("You must select a radio button!");
return false;
}
else {
alert("You selected: " +form1.radio1[thisOne].value+ "\n Radio Button: " + whichOne);
return true;
}
}
</script>
</head>
<body>
<form name=form1>
<input type=radio value="Red" name=radio1>1
<input type=radio value="Yellow" name=radio1>2
<input type=radio value="Green" name=radio1>3
<input type=button value="Validate" onclick="validateRadio(form1)";>
</form>
</body>
</html>


.....Willy

Willy Duitt
10-20-2003, 01:25 AM
Not sure if any of this is helpful, but
I added a variable to capture the ID.

<script language="javascript">
function validateRadio(form1) {
thisOne = -1;

for (i=0; i < form1.radio1.length; i++) {
if (form1.radio1[i].checked) {
thisOne = i;
whichOne = (thisOne+1);
idIt=document.getElementsByTagName("INPUT")[i].id
}
}
if (thisOne == -1) {
alert("You must select a radio button!");
return false;
}
else {
alert("You selected: " +form1.radio1[thisOne].value+ "\n Radio Button: " +whichOne+ "\nThe ID of Radio Button "+whichOne+" is: "+idIt);
return true;
}
}
</script>
</head>
<body>
<form name=form1>
<input type=radio value="Red" name=radio1 ID="My Red Button">1
<input type=radio value="Yellow" name=radio1 ID="My Yellow Button">2
<input type=radio value="Green" name=radio1 ID="My Green Button">3
<input type=button value="Validate" onclick="validateRadio(form1)";>
</form>


....Willy



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum