Search code examples
javascriptimageonmouseoverrollover

Why doesnt my simple javascript rollover work?


Hi guys I have a simple rollover onmouseover effect, I have tried several scripts but none of them work, can anyone tell me why?`

javascript:

<script language="JavaScript" type="text/javascript">
<!--
if (document.images) {
    homebuttonup = new Image();
    homebuttonup.src = "./images/gym-light.png";
    homebuttondown = new Image();
    homebuttondown.src = "./images/buttonright.png";
}

function buttondown(buttonname) {
    if (document.images) {
        document[buttonname].src = eval(buttonname + "down.src");
    }
}

function buttonup(buttonname) {
    if (document.images) {
        document[buttonname].src = eval(buttonname + "up.src");
    }
}
// -->
</script>

and link:

    <a href="index.html" onmouseover="buttondown('homebutton')"       onmouseout="buttonup('homebutton')">
  <img id='Img4Inner' name="homebutton" src='./images/gym-light.png' alt="" />

  </a>

Solution

  • **UPDATE 2 (last one lol) ** you currently have the onmouseout and onmouseover on the a tag, move them to the image tag and it will work:

    you're code:

    <a onmouseout="buttonup('homebutton')" onmouseover="buttondown('homebutton')"        
    href="http://www.[...].com" style="height:120px;width:100px;">
    <img id="Img4Inner" alt="" src="http://[..].com/images/gym-light.png" name="homebutton">
    </a>
    

    Working code:

    <a onmouseout="buttonup('homebutton')" onmouseover="buttondown('homebutton')"        
    href="http://www.[...].com" style="height:120px;width:100px;">
     <img alt="" src="http://[...]/images/gym-light.png"  onmouseout="buttonup(this)" 
         onmouseover="buttondown(this)" name="homebutton" id="Img4Inner">
    </a>
    

    Update: because you're invoking the functions on the anchor tags they need to have a height and a width similar to the following (place your height and width accordingly):

    <a style="height:25px;width:25px;" href="http://www.[...].com" 
    onmouseover="buttondown('homebutton')" onmouseout="buttonup('homebutton')">
    ...
    </a>
    

    and I"m out...

    I just used firebug, edited the HTML with the height and width and it worked fine :

    enter image description here)

    and while I"m sure that will solve the problem.. the doctype is set to <!doctype html> and should be something like what's here (LINK)

    if you would've implemented the below approach, the image would have a height and width, and since that is the image that is being targeting, might make more sense..

    http://jsfiddle.net/ETHaM/2/

    if (document.images) {
        homebuttonup = new Image();
        homebuttonup.src = "http://www.placekitten.com/100/100/";
        homebuttondown = new Image();
        homebuttondown.src = "http://dummyimage.com/100x100/000/fff";
    }
    
    function buttondown(obj) {
        if (document.images) {
            obj.src = eval(obj.name+ "down.src");
        }
    }
    
    function buttonup(obj) {
        if (document.images) {
            obj.src = eval(obj.name + "up.src");
        }
    }
    
    
    <a href="index.html">
    <img id='Img4Inner' onmouseover="buttondown(this)" onmouseout="buttonup(this)" name="homebutton" src='http://www.placekitten.com/100/100/' alt="" />
    </a>