tags:

views:

48

answers:

1

If I have an element nested as:

<ul>
    <li><img src="/blah.jpg" class="removeme" /></li>
    <li><img src="/ole.jpg" class="keepme" /></li>
</ul>

How do I go about removing the entire list containing the img class that has removeme? I've been able to get to

alert($(containername).children('ul').children('li').children().hasClass("removeme"))

returns true but I can't seem to remove the entire <li></li> (and not only the img).

Thanks in advance!

+5  A: 
$('img.removeme').closest('li').remove();

Should do it.

The problem with parents() is that it could potentially remove more than 1 <li> if the list is nested.

Paolo Bergantino
Thank you for pointing out the subtle difference between 'parents' and 'closest'. jQuery never ceases to amaze me.
SolutionYogi
Good call on `.closest` vs `.parents`, I've deleted my earlier answer since yours is an improvement.
Alconja
Actually I needed the parent() implementation more so because I had to first select the right ul. Thanks, both!
Rio
closest() also works for that, what do you mean?
Paolo Bergantino