Click to See Complete Forum and Search --> : Javascript help please


mackins60
February 15th, 2003, 11:57 AM
I have written an array (quantity data from a cookie) to form fields with a for loop e.g.

document.write('<td><input name="quant'+[i]+'" type="text" size="9" value="'+ ard[i] +'"></td>');

I want to allow users to change any of the quantity fields and when an 'Update Quantity' button or link is clicked, it executes a function that will read the form's quant[i] field values back into the quantity array, save to the cookie and then reload the page, which reads the updated cookie (quantity data) and writes the new quantity values back to the form fields along with it's calculations, totals etc.

Everything is working except I can't work out how to read the user changed quantity values back with a for loop from the quant[i] form fields.


for (i=0;i<arraylength;i++)
{

/* the next line is the one I'm having difficulty with!!!
I want to cycle through the form fields e.g. quant0, quant1,
quant2 etc. and overwrite the array values with all the quantity field values.*/

ard[i] = document.form1.quant+i+.value ;


What is the correct syntax for this line?

Thanks in advance,

Steve M

antares686
February 16th, 2003, 09:35 AM
My personal suggestion is don't go into reloading the page to save server round trips or caching issues.

Still store the values in the cookie but read them intially into an array variable. As they change update the array, write to the cookie and recalc the data as needed.

nategrover
February 24th, 2003, 05:27 PM
One way is to use a while loop;

var quantCount = 1;
var theQuant = document.getElementById("quant" + 1);
while(theQuant != null){
/// do your thing
quantCount++;
theQuant = document.getElementById("quant" + quantCount);
}


another is to use the eva() method:

totalQuantFields = //some means of getting the total number

for (var i=0;i<totalQuantFields;i++){
eval("setNewCookieVal(document.Form1.quant" + i + ".value");
}

Good luck

Nate Grover