View Full Version : help with radio buttons

07-07-2002, 01:06 PM
okay this is quite simple i'm sure, but i can't manage to get it.
if somebody could just do stuff for something basic like this, so i can understand how on earth radio buttons are worked with:

there are 3 radio buttons for...say...marital status. when a button is clicked, just alert the status. i tried a script with all the combinations of ifs and elses and radio button names and ids i could think of. here's one of my tries.

function alertstatus(){
if (document.forms.form1.r[0].value=="yes"){
if (document.forms.form1.r[2].value=="yes"){
<form name="form1">
Your marital status:
<input type="radio" name="r" value="no"> Single
<input type="radio" name="r" value="no"> Married
<input type="radio" name="r" value="no"> Divorced
<input type="button" value="submit" onClick="alertstatus()">

WHY doesn't it work?? :(
when i use if-else, it always executes only the else phrase, no matter what button is checked. if i use only ifs, as above, nothing executes. i never managed to make anything work with if-else if-else. (maybe that's not the right syntax?)

07-07-2002, 02:27 PM
just a guessing® here 'cept tryyy n' put thisss line???
<input type="button" value="submit" onClick="alertstatus()">

beforrre your closing </form> tag...

just a goodluck®...

07-07-2002, 09:01 PM
The way radio button values work is whichever one is checked, the corresponding value for that button is the value associated with the entire array. In your case, no matter what button gets checked - the value produced will always be "no". The fact that you click on them does not change their value to "yes".

What you should be doing is determining which button was checked and then grabbing the value of that button - which should be unique. Since radio buttons all have the same name, the value is used to differentiate them. Here's an example of what I mean:

NOTE: when I call this function I'm passing a reference to the radio array which gets referred to in the function as 'rArr'

function alertstatus(rArr){
// -- loop through the array
for (i=0;i<rArr.length;i++) {
// -- when you arrive at the checked one
if (rArr[i].checked) {
// -- alert its value
// -- go no further
<form name="form1">
Your marital status:
<input type="radio" name="r" value="Single"> Single
<input type="radio" name="r" value="Married"> Married
<input type="radio" name="r" value="Divorced"> Divorced
<br><input type="button" value="submit" onClick="alertstatus(this.form.r)">

Hope that helps.

07-08-2002, 06:32 PM
it worked perfectly, i tried the concept on checkboxes as well, and that worked too. thanx a lot:D

is it possible to change the checked button through the script, without user action? i have a form where, once the user checks something and submits, the form disappears. but when the form is invoked again, the radio button which is initially checked is the same one that the user last selected. how can i make sure the button of my choice is always the initially selected one, every time the form is invoked?
the form is just a DIV that's set to visible when invoked and hidden when submitted (in case the info's any use).