ktsirig Posted March 10, 2008 Share Posted March 10, 2008 Hello, I have a js function inside the head of a page of mine which takes 2 arguments that are created dynamically based on what I retrieve from a database. My question is whether I can set the attribute src of the <img> tag through the js function. What I have is: <script type="text/javascript"> function HideShow(myVar, mySecondVar) { if (document.getElementById(myVar).style.display == 'none') { document.getElementById(myVar).style.display = 'inline' -----> mySecondVar.src = "css/images/show.gif" } else { document.getElementById(myVar).style.display = 'none' -----> mySecondVar.src = "css/images/hide.gif" } } </script> The lines with the arrow in front of them do not work, presumably there is a mistake in the syntax because I see no image... I would greatly appreciate any help! Link to comment https://forums.phpfreaks.com/topic/95519-add-image-src-from-javascript-function/ Share on other sites More sharing options...
haku Posted March 11, 2008 Share Posted March 11, 2008 You can set the src with] mySecondVar.setAttribute("src", "css/images/show.gif") This will definitely work with FF, I don't know if it will work with IE though. Try it out and see if it does. If it doesn't, there must be another method of setting the source in IE. Link to comment https://forums.phpfreaks.com/topic/95519-add-image-src-from-javascript-function/#findComment-489019 Share on other sites More sharing options...
nogray Posted March 11, 2008 Share Posted March 11, 2008 if mySecondVar is a string, use document.getElementById(mySecondVar).src = "..."; Link to comment https://forums.phpfreaks.com/topic/95519-add-image-src-from-javascript-function/#findComment-489704 Share on other sites More sharing options...
ktsirig Posted March 11, 2008 Author Share Posted March 11, 2008 Guys thanx! Worked fine!!! Link to comment https://forums.phpfreaks.com/topic/95519-add-image-src-from-javascript-function/#findComment-490006 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.