find image src that :contains?

Charles Web Dev picture Charles Web Dev · Dec 21, 2010 · Viewed 32.3k times · Source

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 ***;

Answer

Gabi Purcaru picture Gabi Purcaru · Dec 21, 2010

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;
});