I have made a dialog that is shown by pressing on a button. I would like to hide it by pressing anywhere else or pressing the escape key. Is there a good way to do this?
Asked
Active
Viewed 143 times
2 Answers
0
Bind a click event to the and in your function do a check. If dialog open, close it.
$(body).bind("click", function() {
if (dialogIsOpen) { // replace dialogIsOpen with your check to see if the dialog is open.
// code to close dialog
}
});
$(body).bind("keydown", function() {
if (keyIsEsc) { // replace keyIsEsc with check for ESC key.
// code to close dialog
}
});

nickytonline
- 6,855
- 6
- 42
- 76
-
Wouldn't this still close it if you clicked on the dialog box? – tallowen Apr 13 '11 at 03:56
-
You could check the target element when the click bubbles up to the body – nickytonline Apr 14 '11 at 16:47