...

View Full Version : If statement using source of an image



supersav144
12-15-2010, 11:57 PM
I am currently trying to write a javascript function which checks whether the source of an image is one thing, if so do this, if not do something else.

Any help is much appreciated.

Below is the code I have tried...


if(document.getElementById("field").src == "images/tick.png"){
alert("this is the tick image");
}

Old Pedant
12-16-2010, 12:10 AM
If you would do

alert(document.getElementById("field").src);
you would see what the *browser* thinks the .src property is.

Turns out, it will be the full URL to the image. e.g.,
http://www.yoursite.com/some/directory/path/images/tick.png

So...

A lot of things you can do: You could simply do

if(document.getElementById("field").src.indexOf("images/tick.png") >= 0 ) {

You could also use .search with a regular expression:


if(document.getElementById("field").src.search( /images\/tick\.png$/i) >= 0 ) {

and so on.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum