enter image description here

I'm trying to write JQuery to remove all divisions with class item ui-draggable... based on the value of the highlighted element. For instance check if the value is less than 100, and if true, remove the division. I have no idea how to do this, however. The structure of all the divisions are the same. The relevant number will always be contained in the 6th sub-element of the division to be removed.

so in pseudo-code I want to write this:

$(document).ready(function(){
    for each division of class .item ui-draggable... {
       if (value of relevant number < 100) {
          .remove(division);
       }
    }
});

4 Answers

1
DaniP On Best Solutions

Try this:

$(document).ready(function(){
    $('.item.ui-draggable').each(function(){
       var val = parseInt($(this).find('.socialMetaCount').text(),10);
       if (val < 100) { 
          $(this).remove();
       }
    })
});
0
void On

So in code you wanted to write is

$(document).ready(function(){
    $(".item ui-draggable").each(function(){
       if (anyNumber< 100 && somethingTrue) {
          $(this).remove();
       }
    }
});
5
David Thomas On

First select the elements you want to assess, and then, if those elements meet the criteria, move up to the relevant element with closest() or parents(), and remove those:

$('em.socialMetaCount').filter(function(){
    return parseInt(this.textContent.trim(), 10) < 100;
}).closest('.item.ui-draggable').remove();

References:

0
fray88 On

I think that what you are looking for is something like this:

$(document).ready(function() {
    $(".ui-draggable").each(function() {
        if (parseInt($(this).find('.socialMetaCount').text()) < 100) {
            $(this).remove();
        }
    });
});