Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

radio button issues 3

Status
Not open for further replies.

travmak

Programmer
Apr 26, 2002
133
US
How do I tell what radio button is selected? I tried copying the value of the radio button with

document.form2.elements[indexnum].value = document.form1.elements[indexnum].value;

but it didn't work.

my two radio buttons are
<input type = &quot;radio&quot; name = &quot;sex&quot; value = &quot;male&quot;>
<input type = &quot;radio&quot; name = &quot;sex&quot; value = &quot;female&quot;>

it always copies &quot;female&quot;

please help
 
yes that was helpful thank you, next question. if i'm doing a loop: for (i = 0; i < form.length; i++) how do i know what ones are radio buttons to put them into a test loop for them?
 
<html>
<head>
<script language=javascript>
function GetRadios()
{
for (i=0;i<document.forms[0].elements.length;i++)
{
if (document.forms[0].elements.type==&quot;radio&quot;)
{
alert(document.forms[0].elements.name)
}
}
}
</script>
</head>
<body>
<form>
<input type=text name=text1>
<input type=radio name=radio1>
<input type=radio name=radio2>
<input type=checkbox name=checkbox1>
</form>
<a href=&quot;#&quot; onclick=&quot;GetRadios(); return false&quot;>get radio names</a>
</body>
</html>
 
your form should be having a name.
if it has a name, change &quot;form&quot; to its name. if your form has a name &quot;form&quot;, please change it to something different. i usually name my forms &quot;frm&quot;. the reason it is not advisable to have your form name as &quot;form&quot; is because it might make javascript cofused between an element name and an element reference.
so if your form name is &quot;frm&quot; your loop should correctly be
Code:
for (i=o; i<frm.sex.length; i++)
if your form name is &quot;test&quot;, your loop should be saying
Code:
for (i=o; i<test.sex.length; i++)
using &quot;sex.length&quot; will isolate the radio buttons from group named &quot;sex&quot; for your loop.

But here's another tip that could be useful in the future:
you can validate a type of form field:
Code:
for (i=o; i<frm.sex.length; i++) {
alert(form.elements[i].type)
}
HTH

--------------------------------------------------
Goals are dreams with deadlines
 
ooops,
my last loop should have been
Code:
for (i=o; i<frm.sex.length; i++) {
alert(frm.elements[i].type)
}
and that is if ny form name is &quot;frm&quot;

sjravee, we were probably posting at the same time. --------------------------------------------------
Goals are dreams with deadlines
 
thank you both.
valeriav - I usualy name my forms something having to do with the information of the form. I just say stuff like document.form.element.name so you don't have to figure out my naming conventions, just trying to make it easier for you guys to read. :)
 
hey,
i was just trying to be helpful ;-)))
*lol* --------------------------------------------------
Goals are dreams with deadlines
 
ok we're getting there.

so we loop to find the radio button that is checked. in this case just to times to find out if male of female is checked.

so lets say male is checked. How do I copy the value &quot;male&quot; to the other form field?

for (i=0;i<document.tv.station.length;i++){
if (document.forms1.element[0].name.checked==true){
document.form2.element[0].value = ?

 
&quot;How do I copy the value &quot;male&quot; to the other form field?&quot;
i am a little bit unclear here: where do you want to copy teh value? into a text field?
Code:
for (i=0;i<document.tv.station.length;i++){
 if (document.tv.station[i].checked == true) {
 document.frm._text_field_name.value = document.tv.station[i].value
 }
}

is that what you mean? --------------------------------------------------
Goals are dreams with deadlines
 
how about writing this code so it is reusable? :)

function getSelectedRadioItem(radio)
{
for (var idx = 0; idx < radio.length; idx++)
{
var radioItem = radio[idx]
if (radioItem.checked)
{
return radioItem
}
}
}

// usage :

document.frm._text_field_name.value = getSelectedRadioItem(document.tv.station).value

Do you find this useful? Gary Haran
 
yeah that's what I mean, here is my actual code. Warning it is a nightmare. I'm trying do do it without knowing the fieldnames so I can use this in multiple pages.

I doesn't work at the moment:

for (radioindexnum = 0; radioindexnum < thisform.elements[indexnum].name.length; radioindexnum++){
if (thisform.elements[indexnum].name[radioindexnum].checked){
document.hiddenvalues.elements[allindexnum].value = thisform.elements[indexnum].name[radioindexnum].value}
 
thanks xutopia that's close to what i'm looking for, but I don't know the formname, or fieldname so vice document.tv.station

something like
document.form2.element.value = getSelectedRadioItem(currentform.element[x]).value

however I can't get it to work. In fact I got some errors using what you had written, weeded it down to this without getting any errors, but the document.write showed &quot;[object]&quot; and ofcourse it didn't save the value

if (thisform.elements[indexnum].type == &quot;radio&quot;){
document.hiddenvalues.elements[allindexnum].value = getSelectedRadioItem(thisform.elements[indexnum])
document.write(document.hiddenvalues.elements[allindexnum]);}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top