I have a popup box that appears after x time, within it is a html form and two text inputs.
I am trying to get a focus listener to close the popup as soon as the user clicks outside the popup.
to do this I have tried setting a focusout on the whole form. This successfully registers when both of the inputs looses focus however it is also registering when it is switching focus from one input to the other.
This of course closes the popup without allowing a the user to fill in the form.
HTML below
<form class="emailForm" name="signup" method="post" >
Email address<br />
<input type="text" id="rm_email" name="rm_email" />
<br />
First name<br />
<input type="text" id="rm_first_name" name="rm_first_name" />
</form>
And the script
$('.emailForm').focusout(function() {
//alert("focus lost");
$('#emailPrompt').fadeOut("fast");
});
Here's the site live if that helps (the popup will appear after a short period)
You need to handle this on click(), as you've said, "close the popup as soon as the user clicks outside the popup".
This should do it:
Here's the demo.
Ref: event.target , closest()