View Full Version : If statement using source of an image

12-16-2010, 12:57 AM
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, 01:10 AM
If you would do

you would see what the *browser* thinks the .src property is.

Turns out, it will be the full URL to the image. e.g.,


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