6

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!

1
  • Can we see your Ajax JS? You don't need to "delete" the html, just select the one you need. Commented Oct 23, 2016 at 10:32

7 Answers 7

4

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());
    }    
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! This is really working, thank you so much! It is possible to add multiple good images in this script:
@AlexandruVorobchevici Glad, i could help. You can mark the answer as done for others' reference.
how can I add multiple images in this code? like good.jpg, good1.jpg, thank you again!
No, I want a white list for specified images! something like gtrskxd.jpg, mfhdj54.jpg, andsj54k2.jpg
Then you can simply add OR condition in the if block
2

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

3 Comments

Why using the not function when you can simple switch >= with < ?
If it is that simple, post it as answer and get a green tick for the simplest answer
I was honestly asking if there is a specific reason or not, if not it is indeed redundant. Don't you agree?
1
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!

3 Comments

All browsers don't serialize CSS rules the same, e.g, your code will work on Safari, but not on chrome nor FF which will convert it to 'url("https://good.jpg")'
@Kaiido Are you sure? How have you checked it? I just test again. It's working good on both Chrome and FF.
I tested it on chrome 54, FF 50, and safari 9.1.3 all on osx and the results are the ones I gave you. It's probable that results do even differ between different versions of the same browser, and on different platforms.
1

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

1 Comment

Even the ones with the correct background-image will be removed here
1

Working example using slice to extract each image url:

$('.first').each(function(){
    var img_url = $(this).find('.test-icon').css('background-image').slice(5, -2);
    if (img_url != "https://good.jpg/"){
        $(this).remove();
    }
});

Comments

0

You could achieve this like below:

var bg = $('first').find('.text-icon').filter(function(bg) {
    return $(bg).css('background-image') === 'url(https://good.jpg)'; 
    //although you should use '/good.jpg') 
)};
bg.hide();

1 Comment

All browsers don't serialize CSS rules the same, e.g, your code will work on Safari, but not on chrome nor FF which will convert it to 'url("https://good.jpg")'
-1

You can try this

if ($('.test-icon').attr('background-image') != "https://good.jpg") {
   $('.first div').remove();
}

If you want to delete the all "first div" class..

1 Comment

This removes pretty much everything in the posted HTML fragment. It's typically a good idea to test code before recommending it to others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.