Reputation: 43
I'm trying to find all elements with class=galleryLink
who's HTML is less than X characters and then remove them from the DOM. Here's my best guess:
if($('.galleryLink').html().length < 95){
this.remove();
}
Upvotes: 0
Views: 416
Reputation: 38345
You could use .filter()
to filter down the elements before removing them:
$('.galleryLink').filter(function() {
return $(this).html().length < 95;
}).remove();
Upvotes: 0
Reputation: 165971
You're looking in the right direction, but you're not quite there. In your example, how would this
ever refer to the elements you want to remove?
You'll have to apply that to all of the matching elements:
$(".galleryLink").filter(function () {
return $(this).html().length < 95;
}).remove();
Note that the .html()
method returns the markup within the element. You may prefer .text()
.
The .filter()
method applies a function to each element in the matched set. If it returns a truthy value, that element remains in the set. So after the filter, you'll be left with all the elements whose content is less than 95 characters in length.
The .remove()
method applies to all elements in the matched set. Since the set has now been filtered down to the elements you want to get rid of, it should do the job.
Upvotes: 4