How to find out if the jquery dialog was closed at startup 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. Using the cancel button, you can easily add a function to the cancel button.

$( '#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 should not be called clicking OK, so I canโ€™t just put it in a close event.

+1
source share
1 answer

After searching without an answer (and therefore I asked a question about the Q & A style), I began to study the close: function( event, ui ) and found a solution using event.originalEvent

 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' ) } 

hope this helps someone, here's the fiddle: http://jsfiddle.net/hbrunar/MXk3a/2/

+7
source

All Articles