Twango Posted February 10, 2010 Share Posted February 10, 2010 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 More sharing options...
justsomeguy Posted February 10, 2010 Share Posted February 10, 2010 You need to use an error console when you're developing. You have a few basic syntax errors. An error console like Firebug for Firefox or IE8's developer tools will tell you why the code is not executing. Link to comment Share on other sites More sharing options...
sumchurch Posted February 12, 2010 Share Posted February 12, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.