Click to See Complete Forum and Search --> : Radio Values
Tricky123
12-19-2002, 04:08 AM
Hello,
How can I return a value for each item in a radio group?
For example, if I have 3 radio's in a group, I would like to return a value for each on even if it was not selected eg
radio1= yes, radio2=no, radio3=no
All i can do at the moment is return the value of the selected item and not all three.
TIA
Hope this will helps you:
<html>
<head>
<title></title>
<meta name="author" content="i.buergisser">
<meta name="generator" content="Ulli Meybohms HTML EDITOR">
<script language="JavaScript">
<!--
function checkradios()
{
radio1 = document.former.radio1.value;
radio2 = document.former.radio2.value;
radio3 = document.former.radio3.value;
alert("Radio1 has the value --> "+radio1 +"\nRadio2 has the value --> "+radio2 + "\nRadio3 has the value --> "+radio3);
}
//-->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<form name="former" onSubmit="checkradios();return false">
<input type="Radio" name="radio1" value="yes">
<input type="Radio" name="radio2" value="no">
<input type="Radio" name="radio3" value="no">
<input type="Submit" name="" value="test">
</form>
</body>
</html>
Tricky123
12-19-2002, 04:47 AM
Swon,
Thanks but this isn't quite how radio's should work. A group of radios should have the same name so that you can only select one. For example, if you had a payment option on a form you would only want a user to select one type of payment which is why you would use a radio. (otherwise a checkbox is best)
my radios would be like this:
<input type ="radio" name="Name" value="FirstName">
<input type ="radio" name="Name" value="MidName">
<input type ="radio" name="Name" value="Surname">
so I would want to know the value for each one eg
FirstName = Yes
MidName = No
Surname =No
etc
Is it that what you want?
<html>
<script language="JavaScript">
<!--
function checkradios(x)
{
lengths = x.elements.length-1;
for(i=0;i<lengths;i++)
{
names = x.elements[i]("name").value;
vals = x.elements[i].checked;
alert(names+" = "+vals);
}
} // you can only have true or false else you must transform into variables
//-->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<form name="former" onSubmit="checkradios(this);return false">
<input type ="radio" name="Name" value="FirstName">
<input type ="radio" name="Name" value="MidName">
<input type ="radio" name="Name" value="Surname">
<input type="Submit" name="" value="test">
</form>
</body>
</html>
Tricky123
12-19-2002, 06:43 AM
Yes that look like what I am after.
I wich to then store the value of each radio in a variable eg
XML = XML +"<FirstName>\n"+ Vals +"</FirstName>"
XML = XML + "<MidName>\n" + Vals + "</MidName>"
XML = XML +"<Surname>\n"+ Vals +"</Surname>"
document.write(XML)
If this works then your solution is what I need. I am unable to test right now as I am not at my home PC with access to the rest of my scripts.
Could I email you offlist?