Skip to content Skip to sidebar Skip to footer

Jqueryui Dialog Replacement For Confirm?

I am trying to override the default confirmation box with the jQueryUI dialog box, and here is my attempt: window.confirm = function (message_confirm) { $(document.createEl

Solution 1:

you could add a callback parameter to the function

window.confirm = function (message_confirm, ok_callback)

fire it on YES

 buttons: { YES: function () { ok_callback(); }}

and go on later like

confirm("message", function(){
    alert('smth');
});

Solution 2:

Because the dialog is asynchronous you can use a deferred approach:

window.confirm = function (message_confirm) {
  var defer = $.Deferred();
  $('<div/>', {title: 'Please confirm', 'class': 'confirm', 'id': 'dialogconfirm', text: message_confirm})
  .dialog({
    buttons: {
      YES: function () {
        defer.resolve("yes");
        $(this).attr('yesno', true);
        $(this).dialog('close');
      }, NO: function () {
        defer.resolve("no");
        $(this).attr('yesno', false);
        $(this).dialog('close');
      }
    },
    close: function () {
      if ($(this).attr('yesno') === undefined) {
          defer.resolve("no");
      }
      $(this).remove();
    },
    draggable: true,
    modal: true,
    resizable: false,
    width: 'auto',
    hide: {effect: "fade", duration: 300}
  });
  return defer.promise();
};

$(function () {
  confirm("Are you sure you want to delete this user?").then(function(yesno) {
    console.log(yesno);
  });
});
<linkhref="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"rel="stylesheet"/><scriptsrc="https://code.jquery.com/jquery-1.12.4.min.js"></script><scriptsrc="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

Solution 3:

Try something like below. It worked for me.

<script>
  $( function() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
        "Delete user": function() {
          alert("test");
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      }
    });
  } );
  </script></head><body><divid="dialog-confirm"title="Please confirm"><p><spanclass="ui-icon ui-icon-alert"style="float:left; margin:12px 12px 20px 0;"></span>Are you sure you want to delete this user?</p></div>

Post a Comment for "Jqueryui Dialog Replacement For Confirm?"