Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

syntax question? 1

Status
Not open for further replies.

starblood

Technical User
Feb 28, 2001
63
GB
Can anyone give me the syntax for replacing the X in the loop statement below for the variable i?

for(var i = 0 ; i <= 4 ; i++)
{

buttonX.src='images/n-buttX.gif'

}
 
Code:
for(var i = 0 ; i <= 4 ; i++)
{
 document.formName.elements["button"+i].src='images/n-butt'+i+'.gif'
}

'hope that helps!

--Dave
 
Nice use of coloring.

*cLFlaVA
----------------------------
Lois: "Peter, you're drunk!"
Peter: "I'm not drunk, I'm just exhausted from stayin' up all night drinking!
 
Yes, it had me thinking "non-denominational holiday season." Inviting!

--Dave
 
Hi Dave (LookingForInfo),

Thanks for taking the time to reply, unfortunately I cannot get your code to work. I noticed that you put a form path around my code, but I want it for images.

Here is the code in full - it is triggered on MouseOver. This works (but only for one button) but I want it to change 4 buttons sumultaneously - hence the loop :

onmouseover="for(var i = 0 ; i <= 4 ; i++){document.images.button1.src='images/n-butt1.gif'}"

I tried to adapt this with variations based on your code but it wouldn't work.

Any further thoughts anyone?
 
Yeah, it should have adapted neatly. Try this:

document.images["button"+i].src='images/n-butt'+i+'.gif'

There's a possibility you'll run into quotation-mark-mayhem when doing this, however, so I would recommend changing the onmouseover action to a function call and put that whole for-statement inside the function.

'hope that helps!

--Dave
 
Hi Dave (LookingForInfo),

Sorry for the delay in replying (been away for a for days). Thanks for the input, your solution worked great with just a little alteration to the quotation marks (as you predicted):

onmouseover="for(var i = 1 ; i <= 4 ; i++){document.images['button'+ i].src='images/n-butt'+i+'.gif'}"

Once again thanks, much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top