Search code examples
jquerycontainssrc

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

Solution

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