Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How to find an image by its src

Tags:

jquery

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"/> 
like image 722
kjgilla Avatar asked May 07 '09 15:05

kjgilla


People also ask

How do I know if an image is selected in jQuery?

You can use a data attribute to mark the image as selected. You can also add a class to visually show that it is selected. Show activity on this post. i.e.

How can I tell if an image is null in src?

Use the getAttribute() method to check if an image src is empty, e.g. img. getAttribute('src') . If the src attribute does not exist, the method returns either null or empty string, depending on the browser's implementation.

How can I get the source of an image in HTML?

To use an image on a webpage, use the <img> tag. The tag allows you to add image source, alt, width, height, etc. The src is to add the image URL. The alt is the alternate text attribute, which is text that is visible when the image fails to load.


2 Answers

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")); 
like image 125
Jared Avatar answered Sep 22 '22 22:09

Jared


Try losing the quotes:

var s = $("img[src=../../images/greendot.gif][name=BS]"); 
like image 42
karim79 Avatar answered Sep 26 '22 22:09

karim79