Results 1 to 3 of 3

Thread: Swapping Image OnClick - Please Help

  1. #1
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Question Swapping Image OnClick - Please Help

    I recently came across the below code - it is for changing the image OnClick - I need to know how to change it back to the original image when you click on the image again.

    Image 1 onclick changes to Image 2 onclick changes to Image 1.....

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
    <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    
    function Swap(obj){
    obj.src='http://www.vicsjavascripts.org.uk/StdImages/Two.gif';
    setTimeout('document.getElementById(\'Img2\').src=\'http://www.vicsjavascripts.org.uk/StdImages/Two.gif\';',2000);
    
    }
    //-->
    </script></head>
    
    <body>
    
    ClickMe >> <img src='http://www.vicsjavascripts.org.uk/StdImages/One.gif' width=100 height="100" onclick="Swap(this);" >
    <img id="Img2" src='http://www.vicsjavascripts.org.uk/StdImages/One.gif' width=100 height="100" >
    </body>
    
    </html>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, it isn't really clear what you are ultimately after. But try this:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
    <title></title>
    <script type="text/javascript">
    
    
    function Swap(obj){
    var other=document.getElementById('Img2');
    obj.src=(Swap.swaped=!Swap.swaped)? 'http://www.vicsjavascripts.org.uk/StdImages/Two.gif' : 'http://www.vicsjavascripts.org.uk/StdImages/One.gif';
    setTimeout(function(){other.src=obj.src;},2000);
    }
    
    </script></head>
    
    <body>
    
    ClickMe >> <img src='http://www.vicsjavascripts.org.uk/StdImages/One.gif' width=100 height="100" onclick="Swap(this);" >
    <img id="Img2" src='http://www.vicsjavascripts.org.uk/StdImages/One.gif' width=100 height="100" >
    </body>
    
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Aug 2007
    Location
    MO USA
    Posts
    106
    Thanks
    37
    Thanked 0 Times in 0 Posts

    Default

    I wanted to swap two images on click. The code you gave works. Thanks a lot.

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
  •