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();
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re looking in the right direction, but you’re not quite there. In your example, how would
thisever refer to the elements you want to remove?You’ll have to apply that to all of the matching elements:
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.