Jump to content

Archived

This topic is now archived and is closed to further replies.

mmosel

document.variable.src - question for you

Recommended Posts

Hi all,

I'm working on a little javascript and I've come upon a lil' snag.

[code]if ( picon == 0){
    //alert (srcimg);
document.imageinstruct1.src = 'ui/showhelp.gif';
}else{
document.imageinstruct1.src = 'ui/hidehelp.gif';
}[/code]

The part that I'm having a problem with is the document.variable.src, where I would like to use a variable that may have different values in that middle spot. As of now I can't get it to work, so I have to type in the actual name value of the image I want to swap. I don't want to have to write multple functions where one should do the trick. Is this possible? If so, what's the trick?

Another example would be:

[code]var myvar = 'main';

document.myvar.src = 'image1.jpg';[/code]

Any ideas or suggestions?

Share this post


Link to post
Share on other sites
[!--quoteo(post=345932:date=Feb 14 2006, 10:53 PM:name=mmosel)--][div class=\'quotetop\']QUOTE(mmosel @ Feb 14 2006, 10:53 PM) [snapback]345932[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi all,

I'm working on a little javascript and I've come upon a lil' snag.

[code]if ( picon == 0){
    //alert (srcimg);
document.imageinstruct1.src = 'ui/showhelp.gif';
}else{
document.imageinstruct1.src = 'ui/hidehelp.gif';
}[/code]

The part that I'm having a problem with is the document.variable.src, where I would like to use a variable that may have different values in that middle spot. As of now I can't get it to work, so I have to type in the actual name value of the image I want to swap. I don't want to have to write multple functions where one should do the trick. Is this possible? If so, what's the trick?

Another example would be:

[code]var myvar = 'main';

document.myvar.src = 'image1.jpg';[/code]

Any ideas or suggestions?
[/quote]


Well, I stumbled onto a solution. Instead of using the name tag I used the id tag, which allowed me to use this: document.getElementById(myvar).src = 'ui/showhelp.gif';

Works...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.