Click to See Complete Forum and Search --> : Loop Question


Breeze
12-30-2003, 11:08 PM
I'm trying to write values to a series of textboxes.

var qty = new Array(50);
var i=0;
while (i<50) {
qty[i] = i;

document.form.box1.value=qty[0];
document.form.box2.value=qty[1];
document.form.box3.value=qty[2];
document.form.box4.value=qty[3];
i=i+1
}


How do I simplify this for a lot more lines. Something like:

document.form.box[i].value=qty[i]

But box[i] is not an object so what do I do? Some sort of string array? Thanks

Khalid Ali
12-30-2003, 11:13 PM
presuming that you have 50 text boxes

var qty = new Array(50);
var i=0;
while (i<50) {
eval('document.form.box'+i+'.value')=i++;
}

there are other ways to get this done as well.....

Breeze
12-30-2003, 11:38 PM
I tried that but got the error "cannot assign to a function result"

Does the eval statement write to the textbox? Not familiar with eval

eval('document.form.box'+i+'.value')=i++;



What would be another way? Trying to learn more.

fredmv
12-30-2003, 11:55 PM
I wouldn't recommend using eval. Check these threads for more details: http://forums.webdeveloper.com/showthread.php?s=&threadid=22920 http://forums.webdeveloper.com/showthread.php?s=&threadid=21625

ray326
12-31-2003, 12:42 AM
Originally posted by Breeze
I tried that but got the error "cannot assign to a function result"

Does the eval statement write to the textbox? Not familiar with eval

eval('document.form.box'+i+'.value')=i++;

What would be another way? Trying to learn more.

while (i<50) {
eval('document.form.box'+i+'.value=' + i);
i++;
}

fredmv
12-31-2003, 01:57 AM
onload = function()
{
var i = 0, f = document.forms[0];

do
{
f['box' + i].value = i;
}

while(++i < f.elements.length);
}

Khalid Ali
12-31-2003, 08:03 AM
did any of the suggestions above did it for you??(ofcourse eval is not the most recomended one,,,its that it cam e to mind first..:D )

Breeze
12-31-2003, 04:22 PM
The eval statement did work fine but I see it is not recommended so I'll try the other method. Thanks.