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

10-01-2005, 03:31 PM
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">
<script type="text/javascript">
function changeImg(iName, imgOne, imgTwo)
if (document.images)
var oImg = eval("document.images."+iName);
if (oImg == imgOne)
oImg.src = imgTwo;
oImg.src = imgOne;
<td><img src="flower1.jpg" name="Img1" id="Img1" alt="" width="157" height="198" onClick="changeImg('Img1','flower2.jpg','flower1.jpg');"></td>

10-01-2005, 03:45 PM
Not tested


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