#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Posts
    162
    Rep Power
    21

    How to change the image?


    Hi guys,

    I need your help, I want to change the image using with a javascript when i press on the up and down keyboard arrow button.


    Code:
    <html>
    <body>
    
    
    <style type="text/css">
     
    body {background:url('images/blue_background.jpg') no-repeat center center fixed;}
     
    </style>
    
    
    <div id="image1" style="position:absolute; overflow:hidden; left:415px; top:157px; width:114px; height:81px; z-index:0"><img src="/images/image_blue.jpg" alt="" title="" border=0 width=114 height=81></div>
    
    
    <script>
    
    document.onkeydown = checkKey;
    
    function checkKey(e) {
    
        e = e || window.event;
    
        if (e.keyCode == '38') {
        
           var path ="/images/image_blue.jpg";
           
             if(document.getElementById("image1").src="/images/image_blue.jpg") {
                document.getElementById("image1").innerHTML=.src="/images/image_yellow.jpg";
             }
             else {
               document.getElementById("image1").src="/images/image_blue.jpg";
    }
        }
        else if (e.keyCode == '40') {
            window.alert('down arrow')
        }
    }
    
    </script>
    
    
    </body>
    </html>

    When I press on the keyboard, it doesn't change the image.

    Does anyone know how i can do this?

    thanks in advance
  2. #2
  3. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    97
    Rep Power
    4
    I am confused by the inconsistent assignments in your if and else clauses.
    What is the ".innerHTML=.src=" all about. Shouldn't it be one or the other.

IMN logo majestic logo threadwatch logo seochat tools logo