/    Sign up×
Community /Pin to ProfileBookmark

simple syntax problem

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:

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@CharlesNov 17.2003 — [font=monospace]for (var i=0; i<4; i++) {document.images['photoslider' + i].src = photos[i]}[/font]
Copy linkTweet thisAlerts:
@PittimannNov 17.2003 — Hi!

First of all your loop is not correct:


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

function doImages(){

for (var x=4; x[COLOR=red][SIZE=4]<[/SIZE][/COLOR]0; x--){

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

}


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

Replace the "[COLOR=red][SIZE=4]<[/SIZE][/COLOR]" with "[COLOR=limegreen][SIZE=4]>[/SIZE][/COLOR]" and you will manage the rest...

Cheers - Pit
Copy linkTweet thisAlerts:
@PittimannNov 17.2003 — Sorry Charles - I hadn't seen your post before posting myself!

Pit
Copy linkTweet thisAlerts:
@CharlesNov 17.2003 — [i]Originally posted by Pittimann [/i]

[B]Hi!



First of all your loop is not correct:



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



function doImages(){

for (var x=4; x[COLOR=red][SIZE=4]<[/SIZE][/COLOR]0; x--){

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

}



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



Replace the "[COLOR=red][SIZE=4]<[/SIZE][/COLOR]" with "[COLOR=limegreen][SIZE=4]>[/SIZE][/COLOR]" and you will manage the rest...



Cheers - Pit [/B]
[/QUOTE]
[font=georgia]Please note, that will not work.[/font]
Copy linkTweet thisAlerts:
@edMXauthorNov 17.2003 — thanks ?
Copy linkTweet thisAlerts:
@PittimannNov 17.2003 — 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
×

Success!

Help @edMX spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...