Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to change images using onClick event handler?

    I am trying to figure how to change image1 to image4 and image2 to image3 by clicking on the images. Here is what I got so far...I know I am missing something but not sure what. I would also like to have an example how to write onMouseOver and onMouseOut in this program. I have viewed some examples but I am still confused. When I test this...I am getting error in Line 42 and 43. I would appreciate your help on this matter. This is what I got so far:

    myImage[1] = new Image ( )
    myImage[1].src = "images/image2.gif"

    myImage[2] = new Image ( )
    myImage[2].src = "images/image3.gif"

    myImage[3] = new Image ( )
    myImage[3] = "images/image4.gif"
    }


    function changeImg( ) {
    if value == 0
    change image1 document.images[1].src = myImages[2]
    {else}
    if value == 1
    change image 0 document.images[0].src = myImages[3]
    {else}
    alert("Houston, we have a problem")
    return false // prevent click from going anywhere

    // -->
    </script>
    </head>

    <body>

    <table border=0>
    <tr>
    <td><a href="Lab4.html"><img src="images/image1.gif" name="image1" width="300" height="150" onClick="changeImg( ); " ></a></td>
    <td><a href="Lab4.html"><img src="images/image2.gif" name="image2" width="300" height="150" onClick="changeImg( ); " ></a></td>
    </tr>
    </table>

    </body>
    </html>
    PJ

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Arrays start at 0 not 1. There may be a better way to do it but this might get you going.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    var myImages=new Image();
    myImages[0] = "images/image2.gif";
    
    myImages[1] = "images/image3.gif";
    
    myImages[2] = "images/image4.gif";
    
    function changeImg() {
    if (document.images[1]){
    document.images[1].src = myImages[2];
    }
    else if (document.images[0]){
    document.images[0].src = myImages[2];
    }
    }
    // -->
    </script>
    </head>
    
    <body>
    
    <table border=0>
    <tr>
    <td><a href="Lab4.html"><img src="images/image1.gif" name="image1" width="300" height="150" onClick="changeImg();return false;" ></a></td>
    <td><a href="Lab4.html"><img src="images/image2.gif" name="image2" width="300" height="150" onClick="changeImg();return false;" ></a></td>
    </tr>
    </table>
    
    </body>
    </html>
    Last edited by _Aerospace_Eng_; 09-22-2005 at 07:12 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Aerospace_Eng_! That is similar what I had before but I could only get one image to change but not the other. Example: first box is letter A and second box is letter B...when I clicked A the second box change B to C which is what I wanted to do. When I clicked on the second box...nothing is happening on the first box which is suppose to change letter to D. Also, I am trying to get it to reverse back to the orginial letters when you click the images again. Am I confusing you yet? LOL sry if I am. I noticed you only put 3 images instead of 4 but I corrected it. So, I am wondering if or do I need to add more to my function? Again, thanks for your time on helping me...I really appreciated!
    PJ


  •  

    Posting Permissions

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