Jump to content

document.variable.src - question for you


mmosel

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?

Link to comment
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...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.