Niklas Fett Niklas Fett - 14 days ago 5
jQuery Question

jQuery .parent() won't trigger hide

my .parent() selector doesn't work with .hide() function.
This is my HTML:

...
<div class="overpopup">
<div class="overpopup--background">

</div>
<div class="overpopup--content">
<img src="http://placehold.it/600x600?text=Overpopup">
</div>
</div>
...


and this part of my jQuery:

$(document).ready(function() {
...
$('.overpopup--background').click(function(){
$(this).parent().hide();
});
});


can anyone spot a mistake I made? I tried diffenent Actions, like adding css worked and those worked. Is hiding the parent even legal in jQuery? And if not, is there a easy workaround?

Answer

Try this code

$("body").on("click", ".overpopup--background", function() {
    $(this).parent().hide();
});

I guess, you are loading this elements via ajax

Comments