Results 1 to 2 of 2

Thread: Multiple onclicks for 1 element?

  1. #1
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple onclicks for 1 element?

    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:

    Code:
    onclick="document.getElementById('origImage').src='2ndImage.png';"
    now I want to be able to click again and go back to the orig image.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    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" 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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •