I used this question to hiding a modal after I clicked outside it.
$(document).mouseup(function (e)
{
var container = $(".messenger");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});
In the comments, somebody rightfully said that you have to add something, if not you can't open the modal again after it has been hidden once
Remembering to use $("YOUR CONTAINER SELECTOR").unbind( 'click', clickDocument ); just beside .hide(). So document don't keep listening for clicks. – brasofilo
So I added a line
$(document).mouseup(function (e)
{
var container = $(".messenger");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
container.unbind( 'click', clickDocument );//THE LINE I ADDED
}
});
But it does not work. If I click again the button, the modal does not reappear anymore.
Rookie in javascript. am I doing something wrong ?
EDIT
The script managing the opening of the modal is not custom. I use hubspot Messenger:
So I only write this code.
var msg;
msg = Messenger().post({
message: 'this si message inside modal',
hideAfter: 2500,
showCloseButton: true,
type: 'skip'
});