Filter word in src image using jquery

I am trying to filter for a specific word ("no-image") in my src image, and if it returns true, I want to delete that particular image, but save the rest of the images.

This is my conclusion:

<div class="product">
  <div class="image">
   <img src="mytee-red.jpg">
   <img src="mytee-blue.jpg">
   <img src="mytee-black.jpg">
   <img src="mytee-no-image.jpg">
 </div>
</div>

This is what I have tried so far, but can't get it to work:

var keyword = "no-image";
 $(".product .image img").filter(function(index) {
    if ($(this).attr("src") == keyword) {
        $(this).remove();
    }
});

Any help would be great !!!

+5
source share
5 answers

You can simplify this in one command -

$(".product .image img[src*='no-image']").remove();

the jQuery attribute contains a selector that helps you pinpoint the exact elements that you want to contain the no-image text anywhere src.

jQuery, . , .

+6
$('.product .image img[src*="no-image"]').remove();

http://api.jquery.com/attribute-contains-selector/

.

+4

, match() ==

var keyword = "no-image";
 $(".product .image img").filter(function(index) {
    if ($(this).attr("src").match(keyword)) {
        $(this).remove();
    }
});

, <img src="mytee-no-image.jpg">, no-image

+3

, filter() :

var keyword = "no-image";
 $(".product .image img").filter(function(index) {
    return $(this).attr("src").match(keyword);
}).remove();
+3

, true. , , false.

.filter(function (index)): , .

var keyword = "no-image";
 $(".product .image img").filter(function(index) {
    return $(this).attr("src") != keyword;
});
+1

All Articles