0
votes

I want to compare if the chosen image equal to the desired image.. Here's the initial code but is not working

function mouseOut(txt){
var imgClick, imgOrig; imgClick = 'images/'+txt+'_onclick.png'; imgOrig   ='images/'+txt+'.png';
if(document.getElementById(txt).src == imgClick){return false;}
else {document.getElementById(txt).src = imgOrig};
}

Then on img

<a href = "#"><img src="images/leistungen.png" alt="leistungen" name="leistungen" width="162" height="38" id="leistungen"
onclick="MM_swapImage('home','','images/home_orig.png','philosophie','','images/philosophie.png','kontakt','','images/kontakt.png','body_layout','','images/body_leistungen.png',0)"
onmouseover="MM_swapImage('leistungen','','images/leistungen_onclick.png',1)"
onmouseout="mouseOut('leistungen')" /></a>

My question again is

if(document.getElementById(txt).src == imgClick)

This is wrong but I want to compare if the current image (mouseover,onclick,onmouseout) is equal to an image filename

let say I have these images... home.png and home_onclick.png the default image is home.png, if onmouseover the image will change to home_onclick, and if on mouseout it will change to home.png if and only if onclick event is not triggered.

Thanks in advance

2

2 Answers

0
votes

SRC will return full path of the image. Hence if you need to compare, put your values of imgClick as full URLs, and not relative. ie imgClick = "http://www.mysite.com/images" +txt+'_onclick.png';

( you can also use window.location.protocol + "//" + window.location.host instead of your sitename )

0
votes

Another approach is to check if the src contains imgClick

if(document.getElementById(txt).src.indexOf(imgClick) > 0){
    return false;
}
else {
    document.getElementById(txt).src = imgOrig;
}