Mathias Spurr Mathias Spurr -3 years ago 232
HTML Question

How to identify when a click is made outside the popup window?

I have a popup window which disappears on click inside, but my purpose is to make it disappear on click outside.

At the moment the popup works fine but it disappears whenever I click inside the window. When I click outside the window, it stays. How do I make it work the oppersite way around?

Code as:

function deselect(e) {
$('.pop').slideFadeToggle(function() {

$(function() {
$('.invite_room_btn').on('click', function() {
if($(this).hasClass('selected')) {
} else {
return false;

$('.close').on('click', function() {
return false;

$.fn.slideFadeToggle = function(easing, callback) {
return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);

And HTML is:

<span class="invite_room_btn">
<div class="messagepop pop">


Answer Source

Your question can be interpreted as "how to identify when the click is made outside the popup window"?

as suggested here, you can work by difference, checking that the click occurred anywhere but the popup window (and eventually some other elements)

This can be achieved as follows:

the HTML code may be something like:

<div id="popup" class="popup"> 
    <div>DIV inside</div>
<button id="open">Open</button>

while the javascript is:

 $(document).ready(function () {

 $('#open').on('click', function () {

 $(document).mouseup(function (e) {
     var popup = $("#popup");
     if (!$('#open').is( && ! && popup.has( == 0) {

Full example with some CSS-styling:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download