BDGapps
BDGapps

Reputation: 3356

Compare an image in javascript

I am trying to compare an image in javascript. If the image is true it will change to a different image. I wrote an if statement but it doesn't seem to work. Does anyone know how I can achieve this?

function test()
{
imageElement = document.getElementById('pic');
if(imageElement.src == "images/cat_12.gif"){

imageElement.src = "images/press2_12.gif";
}else{

}
}

Upvotes: 1

Views: 5799

Answers (2)

kennebec
kennebec

Reputation: 104770

function test(){
    if(imageElement.src.indexOf("images/cat_12.gif") != -1){
       //
    }
}

Upvotes: 2

timdream
timdream

Reputation: 5922

src will be converted to full URL when you read it from Javascript. You may try to extract the filename instead, e.g. img.src.substr(img.src.lastIndexOf('/')).

A better solution would be use classes and CSS background switch between images.

Upvotes: 0

Related Questions