Jump to content

[SOLVED] Image Changer


twilitegxa

Recommended Posts

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

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.