2

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?

tallowen
  • 4,198
  • 7
  • 27
  • 35

2 Answers2

1

To close the dialog on escape key press, just set closeOnEscape as true in the dialog initializer code. To close the dialog on any click outside of the dialog, see this answer.

Community
  • 1
  • 1
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