Results 1 to 3 of 3

Thread: onclick change images

  1. #1
    Join Date
    Dec 2005
    Posts
    133
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default onclick change images

    looking for a code:
    when click on image1: image1 changes to image2
    when click on image2: image2 changes back to image1

    must work in firefox

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there emanuelle,

    try it like this example...
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <style type="text/css">
    div {
        text-align:center;
     }
    #pic{
        border:3px double #999;
     }
    </style>
    
    <script type="text/javascript">
       var c=0;
    window.onload=function() {
    document.getElementById('pic').onclick=function() {
       swapImage();
      }
     }
    function swapImage() {
    obj=document.getElementById('pic');
    if(c==1) {
       obj.src='http://mysite.orange.co.uk/azygous/images/apple0.jpg';
       c=0;
     }
    else {
       obj.src='http://mysite.orange.co.uk/azygous/images/banana.jpg';
       c=1;
     }
    }
    </script>
    
    </head>
    <body>
    
    <div>
    <img id="pic" src="images/apple0.jpg" alt="apple"/>
    </div>
    
    </body>
    </html>
    coothead

  3. #3
    Join Date
    Dec 2005
    Posts
    133
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Default

    ok txs
    now what if I have like 10 images on my page that change when onclick. can I make something dynamic?

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
  •