better ways?

D

Draken

I have a menu of radio buttons with the name myform (thank you to Evertjan.
for your help with my other problem, I have no idea where the error was but
I worked through it from your advice) pretty much one of them HAS to be
selected (it would be hard to avoid it) and my loop cycles through until if
finds the checked button... as I have no way to tell how many items there
will be in the array of menu items I cant put a static number in the loop,
at the moment I have this....
for(i=0;document.menuform.myform != undefined;i++)

as when you reach say document.menuform.myform[10] and there is no 10th it
will return "undefined", but this doesnt seem to be the best of ways to do
it? Is there any exceptions to this way? Any better ways? I tried while
(document.menuform.myform) thinking that reaching an undefined item would be
'false' but as you can guess i caused an infinite loop :(

So basically is there any better/easier ways to write this loop, and are
there any exceptions to this loop?
Thanks
 
M

Markus Ernst

Draken said:
I have a menu of radio buttons with the name myform (thank you to Evertjan.
for your help with my other problem, I have no idea where the error was but
I worked through it from your advice) pretty much one of them HAS to be
selected (it would be hard to avoid it) and my loop cycles through until if
finds the checked button... as I have no way to tell how many items there
will be in the array of menu items I cant put a static number in the loop,
at the moment I have this....
for(i=0;document.menuform.myform != undefined;i++)

as when you reach say document.menuform.myform[10] and there is no 10th it
will return "undefined", but this doesnt seem to be the best of ways to do
it? Is there any exceptions to this way? Any better ways? I tried while
(document.menuform.myform) thinking that reaching an undefined item would be
'false' but as you can guess i caused an infinite loop :(

So basically is there any better/easier ways to write this loop, and are
there any exceptions to this loop?
Thanks


for(i=0; i<document.menuform.myform.length; i++)
 
F

Fred Serry

Richard Cornford said:
<snip>

Did you get that one backwards?


for(i=0;i<document.menuform.elements.myform.length;i++)

("myform" seems a misleading name to give to a set of radio buttons)

Richard.

Holidays starts tomorrow. Seems I need it :)
 
D

Draken

("myform" seems a misleading name to give to a set of radio buttons)

Not sure if that is backwards, I didnt use it as the elements section isnt
needed. I called it myform simply because I had help in a different thread
called "help with onClick" or something like that and I copied and pasted
the code of a working example and didnt change anything, of course I could
change it, but I dont need to, its for a personal website and im the only
person who needs to understand it. The form used to be called menu... which
is a better name for the radio buttons which make up a menu, but I didnt use
that again incase that was some sort of reserved word that was causing an
error.

my only question is the operator... shouldn't it be i<= .. rather than just
< ?? cause if I have 10 array items doesnt the < operator stop at 9?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top