Concat array in element

Hi. I’m fairly new to javascript. I have a code where I’m trying to generate scrollx1 through scrollx100. I can get the array to work with doc.write and I can get one concat variable to work in the element but when I combine them it doesn’t work. Any suggestions as to what I need to add to this code:

var sp=1;
for (sp=1;sp<=100;sp++)
{
var sx = “scrollx”;
var sy = “scrolly”;
var thissx = sx + sp;
var thissy = sy + sp;
theForm[thissx].value = scrollx;
theForm[thissy].value = scrolly;
}

Any help would be much appreciated!!

Yes. I am posting input ids scrollxn/scrollyn. Then I’m saving the scroll positions and assigning them to the id name. After that I echo the id names as window.scroll positions. Works really great for postbacks.
The code that fixed it was:

var len = 100;
for (var sp=1; sp<=len; sp++)
{
var thissx = “scrollx” + sp;
var thissy = “scrolly” + sp;
document.getElementById(thissx).value = scrollx;
document.getElementById(thissy).value = scrolly;
}

Thanks to Glenngv from another forum.

Do you have elements in your form named “scrollx1”, “scrolly1”, etc.?

If possible, it’s preferred to not use id attributes on each of your form elements.
You can easily access the form elements by their name by accessing them through the elements collection of the form.

Before, you were using a non-standard technique that only works on some web browsers:


// this code is unlikely to work
theForm[thissx].value = scrollx;
theForm[thissy].value = scrolly;

Here’s how you access the form elements:


// this standards-based code will work
theForm.elements[thissx].value = scrollx;
theForm.elements[thissy].value = scrolly;