Click to See Complete Forum and Search --> : simple syntax problem


edMX
11-17-2003, 05:48 AM
I'm having trouble with some simple syntax: I have a repeated operation that I want to change to a simple for loop e.g.

document.images.photoslider0.src=photos[0]
document.images.photoslider1.src=photos[1]
document.images.photoslider2.src=photos[2]
document.images.photoslider3.src=photos[3]
document.images.photoslider4.src=photos[4]

where photos[] is an array of image names and
photoslider(x) is the name of the image objects on my page. - I want to change this to something like:

function doImages(){
for (var x=4; x<0; x--){
document.images.photoslider(x).src=photos[x]
}

But this doesn't work because the syntax for photoslider(x) is not correct. So far I've tried:

document.images.(photoslider + x).src=photos[x]
document.images[(photoslider+x)].src=photos[x]
document.images.[photoslider+x].src=photos[x]
document.images[eval(photoslider+x)].src=photos[x]
document.images.photoslider[x].src=photos[x]

and many others, none of which work. Does anyone know the correct syntax for this simple operation?:mad:

Charles
11-17-2003, 06:03 AM
for (var i=0; i<4; i++) {document.images['photoslider' + i].src = photos[i]}

Pittimann
11-17-2003, 06:12 AM
Hi!

First of all your loop is not correct:

-------------------------------------

function doImages(){
for (var x=4; x<0; x--){
document.images.photoslider(x).src=photos[x]
}

-------------------------------------

Replace the "<" with ">" and you will manage the rest...

Cheers - Pit

Pittimann
11-17-2003, 06:14 AM
Sorry Charles - I hadn't seen your post before posting myself!

Pit

Charles
11-17-2003, 06:17 AM
Originally posted by Pittimann
Hi!

First of all your loop is not correct:

-------------------------------------

function doImages(){
for (var x=4; x<0; x--){
document.images.photoslider(x).src=photos[x]
}

-------------------------------------

Replace the "<" with ">" and you will manage the rest...

Cheers - Pit Please note, that will not work.

edMX
11-17-2003, 06:25 AM
thanks :)

Pittimann
11-17-2003, 06:41 AM
Hi Charles!

I was just talking about the loop itself and not the action taken inside. And a loop like
for (var x=4; x<0; x--){do whatever} won't work, whereas
for (var x=4; x>0; x--){do whatever} will work...

Cheers - Pit