...

View Full Version : How can I swap and then restore the original image using onClick



Joe_Bloggs
10-01-2005, 04:31 PM
Hi,
How can I swap one image for another using onClick and then restore the original image using another onClick?
The code that I have been playing around with is listed below. Any help with this would be gratefully appreciated. Many thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<script type="text/javascript">
<!--
function changeImg(iName, imgOne, imgTwo)
{
if (document.images)
{
var oImg = eval("document.images."+iName);
if (oImg == imgOne)
{
oImg.src = imgTwo;
}
else
{
oImg.src = imgOne;
}
}
}
//-->
</script>
</head>
<body>
<table>
<tr>
<td><img src="flower1.jpg" name="Img1" id="Img1" alt="" width="157" height="198" onClick="changeImg('Img1','flower2.jpg','flower1.jpg');"></td>
</tr>
</table>
</body>
</html>

gph
10-01-2005, 04:45 PM
Not tested



onclick="changeImg(this,'flower2.jpg','flower1.jpg')"

function changeImg(img, imgOne, imgTwo)
{
if (!document.images) return false;
if (img.src == imgOne)img.src = imgTwo;
else img.src = imgOne
}


<edit>added missing semicolon</edit>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum