Re: JavaScript - Looping question

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

 Date:  Wed, 9 Aug 2000 13:58:48 -0700
 To:  "hwg languages" <hwg-languages(at)hwg.org>
 References:  hotmail
  todo: View Thread, Original
> is this what you needed? - Cyanide_7

Yes, that was exactly what we needed. (!!!)
All is in order.
We thank you greatly.

_Ken and Sara, the not-quite-so-javascript-ignorant-as-before.



----- Original Message ----- 
From: "Cyanide _7" <leo7278(at)hotmail.com>
To: <webmaster(at)fcs.net>
Cc: <hwg-languages(at)hwg.org>
Sent: Wednesday, August 09, 2000 12:14 PM
Subject: Re: JavaScript - Looping question


> >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 :)
> 
> obviously... i see your problem, and i think solution. first of all, is your 
> iterating variable going to be X or i? lets go with i. now we want to append 
> the value of this variable on the end of the name of the form field. you 
> would build a string and use eval, or you could use the elements array:
> 
> var i = 1;
> // typeof is safer...
> while (typeof(document.forms[0].elements["quantity"+i])!="undefined"){
>    myFunction(document.forms[0].elements["quantity"+i].value);
>    i++;
> }
> 
> is this what you needed? - Cyanide_7
> 
> >
> >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

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