PDA

View Full Version : Multiple onclicks for 1 element?



MilkyTech
03-01-2011, 07:17 PM
Not sure if I worded the subject correctly but I would like to have the onclick for a particular element swap out an image and then a second click on the same element brings back the original image and a third click swaps to the second image again and a 4th click bring back the original, and so on.

In other words: swap back and forth between 2 images each time you click on the same element.

I have this code to switch to the second image:


onclick="document.getElementById('origImage').src='2ndImage.png';"

now I want to be able to click again and go back to the orig image.

vwphillips
03-02-2011, 09:27 AM
<!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" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function Toggle(obj,url){
var swap=window[obj.id+'swap'];
if (!swap){
swap=window[obj.id+'swap']=obj.src;
}
obj.src=obj.src==swap?url:swap;

}

/*]]>*/
</script></head>

<body>
<img id="tst" src="http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg" alt="Img" onmouseup="Toggle(this,'http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg');"/>
</body>

</html>