...

View Full Version : How to change images using onClick event handler?



pj59
09-22-2005, 05:38 AM
I am trying to figure how to change image1 to image4 and image2 to image3 by clicking on the images. Here is what I got so far...I know I am missing something but not sure what. I would also like to have an example how to write onMouseOver and onMouseOut in this program. I have viewed some examples but I am still confused. :confused: When I test this...I am getting error in Line 42 and 43. I would appreciate your help on this matter. This is what I got so far:

myImage[1] = new Image ( )
myImage[1].src = "images/image2.gif"

myImage[2] = new Image ( )
myImage[2].src = "images/image3.gif"

myImage[3] = new Image ( )
myImage[3] = "images/image4.gif"
}


function changeImg( ) {
if value == 0
change image1 document.images[1].src = myImages[2]
{else}
if value == 1
change image 0 document.images[0].src = myImages[3]
{else}
alert("Houston, we have a problem")
return false // prevent click from going anywhere

// -->
</script>
</head>

<body>

<table border=0>
<tr>
<td><a href="Lab4.html"><img src="images/image1.gif" name="image1" width="300" height="150" onClick="changeImg( ); " ></a></td>
<td><a href="Lab4.html"><img src="images/image2.gif" name="image2" width="300" height="150" onClick="changeImg( ); " ></a></td>
</tr>
</table>

</body>
</html>

_Aerospace_Eng_
09-22-2005, 06:00 AM
Arrays start at 0 not 1. There may be a better way to do it but this might get you going.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
var myImages=new Image();
myImages[0] = "images/image2.gif";

myImages[1] = "images/image3.gif";

myImages[2] = "images/image4.gif";

function changeImg() {
if (document.images[1]){
document.images[1].src = myImages[2];
}
else if (document.images[0]){
document.images[0].src = myImages[2];
}
}
// -->
</script>
</head>

<body>

<table border=0>
<tr>
<td><a href="Lab4.html"><img src="images/image1.gif" name="image1" width="300" height="150" onClick="changeImg();return false;" ></a></td>
<td><a href="Lab4.html"><img src="images/image2.gif" name="image2" width="300" height="150" onClick="changeImg();return false;" ></a></td>
</tr>
</table>

</body>
</html>

pj59
09-22-2005, 06:53 AM
Thanks Aerospace_Eng_! That is similar what I had before but I could only get one image to change but not the other. Example: first box is letter A and second box is letter B...when I clicked A the second box change B to C which is what I wanted to do. When I clicked on the second box...nothing is happening on the first box which is suppose to change letter to D. Also, I am trying to get it to reverse back to the orginial letters when you click the images again. Am I confusing you yet? LOL sry if I am. I noticed you only put 3 images instead of 4 but I corrected it. So, I am wondering if or do I need to add more to my function? Again, thanks for your time on helping me...I really appreciated! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum