Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  117] [ 6]  / answers: 1 / hits: 17737  / 9 Years ago, fri, april 3, 2015, 12:00:00

I'm creating a lightbox without using a jquery plugin, and now I'm trying to close it by clicking on the close button or by clicking anywhere else outside of the white area (.white_content)

Jsfiddle Example



<button onclick=document.getElementById('lightbox').style.display='inline';>
Show lightbox
</button>

<!-- LIGHTBOX CODE BEGIN -->
<div id=lightbox class=lightbox style=display:none>
<div class=white_content>
<a href=javascript:void(0) onclick=document.getElementById('lightbox').style.display='none'; class=textright>Close</a>
<p>Click anywhere to close the lightbox.</p>
<p>Use Javascript to insert anything here.</p>
</div>
</div>
<!-- LIGHTBOX CODE END -->


Although it's not just like I want it. I want it to close only if I click on the dark area of the lightbox and not on the white container (.white_content), I've heard that event.propagation can be a bad thing to use, so here's how I'm closing the lightbox



$(document).on('click', function(event) {
if (!$(event.target).closest('button').length) {
$(.lightbox).hide();
}
});

More From » jquery

 Answers
3

you can change you condition bit like below



$(document).on('click', function(event) {
if ($(event.target).has('.white_content').length) {
$(.lightbox).hide();
}
});

[#67205] Wednesday, April 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominickmackenziet

Total Points: 583
Total Questions: 101
Total Answers: 117

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
dominickmackenziet questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Fri, Feb 12, 21, 00:00, 3 Years ago
;