Jump to content

Updating Img?


Twango

Recommended Posts

why isnt this replacing the image?<html><body onLoad="setBg()"><script type="text/javascript">var morning=morningimg.png;var day="http://w3schools.com/js/b_pink.gif";var dusk=duskimg.png;var night=nightimg.png;function setBg(){document.getElementById('ict').src=day;}</script>What's Up? <img src="" id="ict"/><h1 onclick="setBg()">H1</h1></body></html>

Link to comment
Share on other sites

why isnt this replacing the image?<html><body onLoad="setBg()"><script type="text/javascript">var morning=morningimg.png;var day="http://w3schools.com/js/b_pink.gif";var dusk=duskimg.png;var night=nightimg.png;function setBg(){document.getElementById('ict').src=day;}</script>What's Up? <img src="" id="ict"/><h1 onclick="setBg()">H1</h1></body></html>
ONE! You cannot be so sloppy. Unquoted stuff (like morningimg.png) is an undefined variable, not the name of a file!TWO -- I am newer here than you. I only know document.getElementById(id).innerHTMLbut it certainly serves me for what you are doing here.Your <src="".... looks like it wouldn't work on a good day! I changed your function code to:function setBg(){a='<img src="'+day+'">';document.getElementById('ict').innerHTML=a;}in order to make A into a an HTML tag.Now define the place in your HTML portion for the function to put the constructed tag...What's Up? <div id="ict"></div><script>setBg();</script>THREE - Maybe you redefined h1 as your anchor tag. But it is just as good to do this:<a href="#" onclick="setBg();">H1</a>
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...