Jump to content

add image src from javascript function


ktsirig

Recommended Posts

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
Share on other sites

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
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.