Alexandru Vorobchevici Alexandru Vorobchevici - 1 month ago 8
HTML Question

jquery/javascript remove div if jpg not match

I have this html response

<div class="first div">
<div class="second">
<div class="title">Hi</div>
<div class="test-icon" style="background-image: url(https://1.jpg);">
</div>
</div>
</div>
</div>
<div class="first div">
<div class="second">
<div class="title">Hi</div>
<div class="test-icon" style="background-image: url(https://12.jpg);">
</div>
</div>
</div>
</div>
<div class="first div">
<div class="second">
<div class="title">Hi</div>
<div class="test-icon" style="background-image: url(https://123.jpg);">
</div>
</div>
</div>
</div>
<div class="first div">
<div class="second">
<div class="title">Hi</div>
<div class="test-icon" style="background-image: url(https://good.jpg);">
</div>
</div>
</div>
</div>


It is possible to delete the all "first div" class if the background image is not this url: https://good.jpg so the final response will be

<div class="first div">
<div class="second">
<div class="title">Hi</div>
<div class="test-icon" style="background-image: url(https://good.jpg);">
</div>
</div>
</div>
</div>


I would be grateful for any assistance, thank you!

Answer

Here is a WORKING FIDDLE of your example:

$('.first').find('.test-icon').each(function(){
    if($(this).css('background-image').indexOf("good") == -1){
      $(this).closest('.first').remove();
    }    
});

Check FIDDLE to add multiple images

$('.first').find('.test-icon').each(function(){
    if($(this).css('background-image').indexOf("good.jpg") == -1 &&
        $(this).css('background-image').indexOf("good1.jpg") == -1 &&
          $(this).css('background-image').indexOf("good2.jpg") == -1){
      console.log($(this).closest('.first').remove());
    }    
});