user2651804
user2651804

Reputation: 1614

JQuery how to remove element based on content of sub-element

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

Upvotes: 1

Views: 104

Answers (4)

fray88
fray88

Reputation: 820

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

Upvotes: 0

David Thomas
David Thomas

Reputation: 253318

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:

Upvotes: 5

DaniP
DaniP

Reputation: 38252

Try this:

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

Upvotes: 1

void
void

Reputation: 36703

So in code you wanted to write is

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

Upvotes: 0

Related Questions