Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find image src that :contains?

Morning all,

I have a list of images like so:

<ul id="preload" style="display:none;"> <li><img src="afx4000z-navy-icon-1_thumb.jpg"/></li> <li><img src="afx4000z-green-icon-1_thumb.jpg"/></li> </ul> 

Using jQuery how find all image src's within ul#preload that contain a specific string eg."green"

something like...

var new_src = jQuery('#preload img').attr('src')*** that contains green ***; 
like image 947
Charles Web Dev Avatar asked Dec 21 '10 10:12

Charles Web Dev


People also ask

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.

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.

What is data image SRC?

data-src is used when lazy loading to prevent the default image from loading when the page loads. Most lazy loading libraries will use intersection observer and copy the data-src value to src when it's time to load the image.


2 Answers

You need to use the *= selector:

jQuery('#preload img[src*="green"]') 

If you want it to be case insensitive, it will be a bit more difficult:

var keyword = "green"; $("#preload img").filter(function(keyword) {     return $(this).attr("src").toLowerCase().indexOf(keyword.toLowerCase()) != -1; }); 
like image 117
Gabi Purcaru Avatar answered Sep 19 '22 02:09

Gabi Purcaru


You can use an attribute-contains selector ([attr*=value]), like this:

jQuery('#preload img[src*=green]') 
like image 43
Nick Craver Avatar answered Sep 19 '22 02:09

Nick Craver