better ways?

Discussion in 'Javascript' started by Draken, Jul 3, 2003.

  1. Draken

    Draken Guest

    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
     
    Draken, Jul 3, 2003
    #1
    1. Advertising

  2. Draken

    Markus Ernst Guest

    "Draken" <> schrieb im Newsbeitrag
    news:3f03f83b$0$...
    > 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++)

    --
    Markus
     
    Markus Ernst, Jul 3, 2003
    #2
    1. Advertising

  3. Draken

    Fred Serry Guest

    "Richard Cornford" <> schreef in bericht
    news:be14m6$hrq$1$...
    > "Fred Serry" <> wrote in message
    > news:be0vq1$l5m$...
    > <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 :)
     
    Fred Serry, Jul 3, 2003
    #3
  4. Draken

    Draken Guest

    > ("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?
     
    Draken, Jul 3, 2003
    #4
  5. Draken

    Markus Ernst Guest

    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?
    >
    >
    >
     
    Markus Ernst, Jul 3, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bencsik
    Replies:
    2
    Views:
    878
  2. Andrew Thompson
    Replies:
    8
    Views:
    163
    Premshree Pillai
    Jun 7, 2005
  3. Jonathan Groll
    Replies:
    1
    Views:
    323
    Kouhei Sutou
    Jun 27, 2009
  4. phal
    Replies:
    4
    Views:
    97
  5. geoffcox75
    Replies:
    7
    Views:
    115
    Dr J R Stockton
    Jan 21, 2009
Loading...

Share This Page