Click to See Complete Forum and Search --> : Radio button group with only 1 button?


TBor
05-30-2003, 11:45 AM
OK, hello again to everyone. This is an odd one, hopefully someone here can help me.

I've got an ASP script that generates a group of radio buttons. These radio buttons all have the same name/id, but different values.

On a certain event, I want to check the group of radio buttons to see which one is selected. I have no problem writing the "For" loop to handle this. However, because of how the group of radio buttons is generated, there is a very real possibility that the "group" will consist of only 1 radio button. This messes up my "for" loop, as I can't check the Length property of a group of 1 radio button.

Here's an example of what I mean:

<script language="VBScript">
sub Click
set radiogroup = document.formname.radioname
for i=0 to radiogroup.length
if radiogroup(i).checked then
RadioVal = radiogroup(i).value
exit for
end if
next
end sub
</script>
...
<form name="formname">
<input type="radio" name="radioname" value="1">
<input type="radio" name="radioname" value="2">
<input type="radio" name="radioname" value="3">
<input type="button" onClick="Click">
</form>

In the script above, radiogroup.length = 3. However, if my form looked like this (which is possible in my code):

<form>
<input type="radio" name="radioname" value="1">
<input type="button" onClick="Click">
</form>

...then I get the error "VBScript Runtime Error: Object doesn't support this property or method: 'radiogroup.length' was not handled". This error is not limited to the Length property - if I change the For loop to "For i=1 to 100"', I still get an error on all lines containing "radiogroup(i)" (I guess that's because radiogroup is not a group in this case)

I'm using radio buttons here because I need to ensure only 1 of the items in the group is selected at any one time, no matter if the group has 1 element or 1000 elements.

So, after all that description, my question:
(1) is there a way to detect if my radio button group only has 1 button in it? If so, I could use an if...else structure for handling the 1 button...many buttons situations.
(2) if #1 is no, then can anyone think of another way to give me the functionality of the radio button (only 1 checked at a time) without using radio buttons?

Hopefully I've explained myself well enough here. Thanks in advance for your help.

Thanks,
TBor

Jona
05-30-2003, 02:03 PM
I don't know how to do it in VBScript, but I will try... In Javascript, you would loop through all of the form elements (Document.formname.elements(i) I think) and say If Document.formname.elements(i).Type = "radio" Then set radiogroup = radiogroup + 1

Jona

TBor
05-30-2003, 02:25 PM
Thanks Jona.

After I posted the original message, I thought of another way to make this work. Yours is equally good for my purposes.

Seeing as I was generating radio buttons dynamically, I just incremented a variable every time I created a new radio button. Then, I passed that variable to my subroutine. Don't know why I didn't think of that earlier.

Thanks for your help though,
TBor

TBor
06-02-2003, 10:50 AM
Thanks Dave!