Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out if jquery dialog was closed on escape and execute some code

I have a jQuery dialog and I need to execute some_code() when I press Esc or click the Cancel button. The latter is easy to implement, I just add define a property of the buttons object:

$('#mydialog').dialog({

  closeOnEscape: true,

  close: function(event, ui) {
    //some_code();
    $(this).dialog('destroy')
  },

  buttons: {
    "OK": function() {
      $(this).dialog("close");
    },
    "Cancel": function() {
      some_code();
      $(this).dialog("close");
    }
  }
});

But how can I execute some_code() after pressing Esc? This function must not be called clicking the OK button, so I cannot simply place it in the close event.

like image 716
Vickel Avatar asked Jan 19 '14 15:01

Vickel


1 Answers

After googling around without finding an answer, I started looking into close: function(event, ui) part and found the solution using event.originalEvent (JSFiddle):

close: function(event, ui) {
  // some_code();
  if (event.originalEvent) {
    // triggered by clicking on dialog box X or pressing ESC
    // not triggered if a dialog button was clicked
    some_code();
  }
  $(this).dialog('destroy')
}
like image 139
Vickel Avatar answered Sep 21 '22 02:09

Vickel