twilitegxa Posted October 21, 2008 Share Posted October 21, 2008 I am trying to do an image changer, but I have done something wrong and can't figure out what it is. I have an image at the top as a logo, but the second image is the one I want to do the changing, but the logo is changing instead. What can I do to stop this from happening? Here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Prairie School District - What's New</title> <script type="text/javascript"> /* <![CDATA[ */ var curImage="image1"; var changeImages; function schoolAd() { if (curImage == "image2") { document.images[0].src = "image1.jpg"; curImage = "image1"; } else { document.images[0].src = "image2.jpg"; curImage = "image2"; } } /* ]]> */ </script> </head> <body onload="var changeImages=setInterval('schoolAd()',2000);"> <h1 align="center"><img src="PSD-Logo.gif" alt="Prairie School District Logo" width="577" height="62"></h1> <table align="right"> <tr> <td><img src="image1.jpg" height="60" width="370" alt="Advertising image for Prairie School District" /></td></tr></table><br /> <h2>What's New</h2> <center> Back to <a href="KulaE_IMD2151_U1IP1.html">Home</a> Page </center> <p align="center"><strong><em>Liz Kula - IMD2151 - Unit 3 IP</em></strong></p> </body> </html> Quote Link to comment Share on other sites More sharing options...
web_loone_08 Posted October 22, 2008 Share Posted October 22, 2008 needs to be like this: document.images[1].src; Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 22, 2008 Share Posted October 22, 2008 Or give the image an ID and use document.getElementById('myid').src=... Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted October 26, 2008 Author Share Posted October 26, 2008 I tried the document.images[1].src, but then images didn't change at all when I used it. As for the second suggestion, if i name the first image and change the code, how would I change the second set of code because the other image wouldn't have an id or name? If I changed the code to: function schoolAd() { if (curImage == "image2") { document.getElementById('image1').src = "image1.jpg"; curImage = "image1"; } else { document.images[0].src = "image2.jpg"; curImage = "image2"; } What would I do to change the second part of the statement? I don't have image2 on the page, so I have no way to set a name or id for it. I tried naming image1 with the name="image1" attribute but that didn't help either. Any other help? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted October 27, 2008 Share Posted October 27, 2008 You could add the id attribute to the images thats how the getElementById() function works example document.getElementById('image1').src="new_image.jpg" would change the image <img id='image1' src='old_image' /> Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted October 29, 2008 Author Share Posted October 29, 2008 I somehow got it to work by just changing the document.images[0].src both to domument.images[1].src and now it works just fine. I don't know why it wouldn't work the first time i tried it, but now it does, so Yay! Thanks for all your help guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.