Jquery / javascript remove div if jpg doesn't match
I have this HTML code:
<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 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 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 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>
Is it possible to delete all elements with the class "first div" if there is no background image: url(https://good.jpg);
? so the final answer 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>
I would be grateful for any help, thanks!
+6
7 answers
Here is the 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()); } });
+4