Javascript questions

puffpio

Golden Member
Dec 21, 1999
1,664
0
0
I have a set of 2 radio buttons with values "a" and "b"
(ie <input type="radio" name="foo" value="a"> etc etc)

Now I have a javascript function which takes that radio button foo and does a

if (foo.value =="a")
//do this
else if (foo.value == "b")
//do this


the problem is those comparisons are not working and it never gets to either codepath. I tried setting the values to numbers instead of strings..I tried sending in foo.value into the javascript function instead of sending in foo and getting value there.....nothing works...any ideas?
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Try:

if (document.<form name>.<radio button name>[0].value=="a") {
//Do something..
}

else if (document.<form name>.<radio button name>[1].value=="b") {
//Do something...
}

Make sure that all your radio buttons has the same name (eg. all your buttons has the declaration name="foo").

 

puffpio

Golden Member
Dec 21, 1999
1,664
0
0
yeah but that doesn't tell me which one is selected...i think i have an idae tho..
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
You'll have to iterate over the group and check the "checked" property. When you find which one is checked you can get it's value.

var value = null;
if (document.<form name>.<radio button name>.length) {
for (var b = 0; b < document.<form name>.<radio button name>.length; b++)
if (document.<form name>.<radio button name>\[b\].checked)
value = document.<form name>.<radio button name>\[b\].value;
}

Or something like that. And yes, it's kind of a pain. There has got to be an easier way to do this

By the way, long time no talk Pio. :)