mmosel Posted February 15, 2006 Share Posted February 15, 2006 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 https://forums.phpfreaks.com/topic/3433-documentvariablesrc-question-for-you/ Share on other sites More sharing options...
mmosel Posted February 15, 2006 Author Share Posted February 15, 2006 [!--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 https://forums.phpfreaks.com/topic/3433-documentvariablesrc-question-for-you/#findComment-11755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.