cup_of cup_of - 2 months ago 3
HTML Question

Hide parent div using jQuery issue

Hello I need some help with hiding the parent div using jQuery.

Here is my html:

<div class="team-member">
<div class="team-member-info">
<h3 class="team-member-name">Ann</h3>

Here is the jQuery I have so far:

if ($('.team-member-info .team-member-name:contains("Ann")').length > 0) {

Basically what I am trying to achieve is, If ".team-member-name" contains the word "Ann", get rid of the entire div, which is ".team-member"

The first line of the jQuery works fine but the second line doesn't seem to work.

Thanks in advance!


You don't even need if-block here. Just select and hide:

$('.team-member-info .team-member-name:contains("Ann")').closest('.team-member').hide()

If jQuery doesn't find any .team-member-name containing "Ann" inside it won't hide anything anyway.

Also note, that because you want to walk two levels up, you can't use $.fn.parent (only first level ancestor). Instead you should either use $.fn.parents or $.fn.closest.