Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Dialog - halt close event

Is there a way to stop a close event for a jQuery UI Dialog?

I have a dialog modal with a form. When the user closes the dialog, I'd like to prompt "Continue without saving changes?" [Yes] [No]. The [Yes] button continues and closes the dialog as expected. The [No] button will stop the close event and keep the dialog open.

Is this possible?

like image 355
gurun8 Avatar asked Jul 18 '11 15:07

gurun8


2 Answers

Yes, you can use the beforeClose option. From the docs:

This event is triggered when a dialog attempts to close. If the beforeClose event handler (callback function) returns false, the close will be prevented.

Code examples

Supply a callback function to handle the beforeClose event as an init option.

$( ".selector" ).dialog({
   beforeClose: function(event, ui) { ... }
});
like image 152
Justin Ethier Avatar answered Nov 15 '22 17:11

Justin Ethier


Justin answer works fine to show the modal. If you want to cancel the closing, your function should return false, like that:

$( ".selector" ).dialog({
   beforeClose: function(event, ui) 
    { return check_if_unsaved_changes(..); }
});

check_if_unsaved_changes should return true if the dialog should close and false if it should not close.

like image 25
OritK Avatar answered Nov 15 '22 18:11

OritK