jQuery: How to find an image by its src

I need to find an img by name and src. I have been trying the following to no avail.

var s = $("img[src='/images/greendot.gif'][name='BS']");

The html:

<img alt="This item is active." name="BS" src="/images/greendot.gif"/>

vs

<img alt="This item is not active." name="BS" src="/images/spacer.gif"/>

Solution 1:

without seeing the html I would say check your path.

$("img[src$='greendot.gif'][name='BS']")

given the following HTML:

<img src="http://assets0.twitter.com/images/twitter_logo_header.png" name="logo" />

this jquery worked:

var x = $("img[src$='twitter_logo_header.png'][name='logo']");
alert(x.attr("src"));

Solution 2:

Try losing the quotes:

var s = $("img[src=../../images/greendot.gif][name=BS]");

Solution 3:

Browsers add full path to src, href and similar attributes. So even though element is defined with relative path, in DOM 'src' attribute contains domain name and full path. Using [src$="..."] syntax or specifying full domain name seem to be only options for finding images by src with jQuery.


I might be wrong here. src property of image object reports full path, but the attribute should contain path as specified in the code. [src="relative/path"] works in the latest jQuery version, maybe it was an early version where it didn't work.