javascript Confirm replacement with return true/false

Jason picture Jason · Aug 10, 2011 · Viewed 7.4k times · Source

Since jquery UI dialog does not support returning true/false, I need some other way to replace a javascript confirm.

It has to return true/false, so that my validation processes in javascript will run:

 var where_to_coupon = confirm(pm_info_msg_013);
if (where_to_coupon== true) {
doSubmit=true;
 return doSubmit;

Answer

Ariel Popovsky picture Ariel Popovsky · Aug 10, 2011

The only way I know of doing that is passing a callback to the function.
The problem you face is that JQuery UI will not block the execution like confirm to wait for user input so you need to open the dialog and when the user clicks an answer act accordingly.

If you use Jquery UI dialog you can bind the callback functions to the buttons.

For instance:

myConfirm("Are you sure?", function(){ [YES CODE] }, function(){ [NO CODE] });

Your custom confirm will look like this:

var myConfirm = function(msg, yesAction, noAction){
  $.dialog{
     [CODE],
     buttons: {
            yes: yeasAction,
            no: noAction
            }
     };
};