By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

better ways?

P: n/a
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[i] != 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
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Draken" <lo***************@hotmail.com> schrieb im Newsbeitrag
news:3f**************@echo-01.iinet.net.au...
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[i] != 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++)

--
Markus
Jul 20 '05 #2

P: n/a

"Richard Cornford" <Ri*****@litotes.demon.co.uk> schreef in bericht
news:be*******************@news.demon.co.uk...
"Fred Serry" <fr***************@planet.nl> wrote in message
news:be**********@ID-108377.news.dfncis.de...
<snip>

Did you get that one backwards?
for(i=0;i<document.menuform.myform.elements.length ;i++)


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 :)
Jul 20 '05 #3

P: n/a
> ("myform" seems a misleading name to give to a set of radio buttons)
Richard.


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?

Jul 20 '05 #4

P: n/a
if i starts at 0 you have 10 values that are < 10, that's the trick...

--
Markus

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?

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.