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 "first div" class if the background image is not: 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>
I would be grateful for any assistance, thank you!
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());
}
});
Filter the divs having content with good.jpg
and remove the negation with not()
and remove()
:
$('.first.div').not($('.first.div').filter(
function(index, element)
{
return $(element)
.find(".test-icon")
.css('background-image').indexOf("good") >= 0
}
)).remove();
var first_div = $('div.first.div');
for (var i = 0; i < first_div.length; i++) {
var background_image = $(first_div[i]).find('.test-icon').css('background-image');
if (background_image !== 'url(https://good.jpg)') {
$(first_div[i]).remove();
}
}
Hope this help!
You need traverse all .text-icon elements with each function of JQuery.
$( "div.first .text-icon" ).each(function( ) {
if( $( this ).css('background-image')!='url(https://good.jpg)' ) {
$( this ).remove();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With