...

View Full Version : help with radio buttons



ASAAKI
07-07-2002, 12: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.

<script>
function alertstatus(){
if (document.forms.form1.r[0].value=="yes"){
alert('single');
}
if(document.forms.form1.r[1].value=="yes"){
alert('married');
}
if (document.forms.form1.r[2].value=="yes"){
alert('divorced');
}
}
</script>
<body>
<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
</form>
<BR>
<input type="button" value="submit" onClick="alertstatus()">
</body>


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?)

justame
07-07-2002, 01:27 PM
asa...
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®...

boywonder
07-07-2002, 08:01 PM
Hi,
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'

<script>
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
alert(rArr[i].value);
// -- go no further
break;
}
}
}
</script>
<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)">
</form>

Hope that helps.

ASAAKI
07-08-2002, 05: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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum