Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Hide child <div> when user clicks inside parent <div>

Tags:

jquery

I have the following code and would like to hide a DIV when the user clicks anywhere inside it's parent DIV. If the user clicks outside of the parent DIV then nothing would happen.

<div id="BodyField">
    <div class="video-field-new"></div>
</div>

So I would like the DIV with "video-field-new" to hide when user clicks inside of #BodyField.

Background: I have a small banner that lays over a video in the corner when the video is new, but I do not want it to block the video player when the user plays the video. So ideally when the user clicks to play the video, which is inside the #BodyField then the banner will hide.

like image 562
Dan Avatar asked Dec 21 '22 09:12

Dan


2 Answers

$('#BodyField').on('click', function() {  
    $(this).children('.video-field-new').hide();
});

You will also need to prevent the click event from the .video-field-new from propagating up to its parent, otherwise clicking the video to play will cause itself to be hidden.

$('#BodyField .video-field-new').on('click', function(e) {  
    e.stopPropagation();
});
like image 131
BenM Avatar answered Jan 17 '23 20:01

BenM


$(".video-field-new:parent").click(function() {
    $(this).find(".video-field-new").hide();
});
like image 26
dwild Avatar answered Jan 17 '23 19:01

dwild