Re: JavaScript - Looping question

by "Ken" <webmaster(at)fcs.net>

 Date:  Wed, 9 Aug 2000 10:46:33 -0700
 To:  "hwg languages" <hwg-languages(at)hwg.org>
 References:  hotmail
  todo: View Thread, Original
The problem Sara and I are having, is getting
Javascript to resolve and recognize dynamically-generated 
form names, like this:

The page pulls products from a database.
INPUT field names for quantity are assigned like so:
quantity1, quantity2, quantity3 ...

We want javascript to do something with each of 
the "quantity" elements.

var X = 1;
while ( document.forms[0].quantityX ){    // do while the forms are defined or exist
  myFunction( document.forms[0].quantityX.value );
  i++;
}

Obviously, this gives a nice error about document.forms[0].quantityX
not being an object. Basically what we need to know is how to
accomplish the following (without the error :)

alert(document.forms[0].quantity1.value);    // this is defined
var X = 1;
alert(document.forms[0].quantityX.value);    // this gives an error


Thanks for any assistance,
_Ken Creason
webmaster(at)fcs.net




----- Original Message ----- 
From: "Cyanide _7" <leo7278(at)hotmail.com>
To: <r.bhaskar(at)compserv.gla.ac.uk>
Cc: <saraw(at)fcs.net>; <hwg-languages(at)hwg.org>
Sent: Wednesday, August 09, 2000 8:17 AM
Subject: Re: JavaScript - Looping question


> execution wise, a for loop is merely a while loop with an iteration exit. 
> so:
> 
> for(var i = 0; i < 10; i++){
>   // do stuff
> }
> 
> is comparable to:
> 
> var i = 0;
> while (i < 10){
>   // do stuff
>   i++;
> }
> 
> with the execption that 'i' is defined within the scope of the loop in a for 
> loop, and not with the while loop (unless previously defined). for loop is 
> merely a convenience in most cases and helps keep code 'clear' when trying 
> to understand a loop's intentions. as far as i can tell, there are no 
> advantages/disadvantages of one over the other aside from readability of 
> code, its your call. - Cyanide_7
> 
> >Hi Sara,
> >
> > > Has anyone ever had to do this or have any suggestions regarding the
> > > advantages and/or disadvantages of using "for" or "while"
> >
> >While I'm not familiar with iHTML (in fact, I've never heard of it), in
> >general programming terms, you usually use a FOR loop if you
> >know in advance how many iterations of the loop that you have to
> >do, and a WHILE loop if you don't.
> >
> >HTH,
> >Raj.
> 
> ________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
> 
> 

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA