find image src that :contains?

34,610

Solution 1

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

Solution 2

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

jQuery('#preload img[src*=green]')
Share:
34,610
Charles Web Dev
Author by

Charles Web Dev

Updated on June 19, 2020

Comments

  • Charles Web Dev
    Charles Web Dev almost 4 years

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