18

Can I write a custom confirm box in JavaScript that, instead of the default OK and CANCEL button, shows a SAVE and DELETE button?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70
  • 1
    possible duplicate of [custom choices in javascript confirm dialog.](http://stackoverflow.com/questions/1800033/custom-choices-in-javascript-confirm-dialog) – kennytm Oct 20 '10 at 09:12
  • 2
    I recommend not to use JavaScripts `confirm()`, `alert()` or `prompt()` commands, since those are blocking the whole browser thread (in most browsers). You should use a JavaScript-UI alternative. – jwueller Oct 20 '10 at 09:35

3 Answers3

17

Use the jQuery UI Dialog Box for this.

You can do something like this:

JS:

$(function() {
  $("#dialog-confirm").dialog({
    resizable: false,
    height: "auto",
    width: 400,
    modal: true,
    buttons: {
      "Do it": function() {
        $(this).dialog("close");
      },
      Cancel: function() {
        $(this).dialog("close");
      }
    }
  });
});
<link href="https://jqueryui.com/jquery-wp-content/themes/jquery/css/base.css" rel="stylesheet"/>
  
<link href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>



<div id="dialog-confirm" title="Is it treason, then?">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Am I the Senate?</p>
</div>
Malavos
  • 429
  • 3
  • 12
  • 27
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
7

Not with the native JavaScript confirm box. You could use one of the many JavaScript UI components that emulate modal dialogs to do this instead.

Here's an example: https://jqueryui.com/dialog/#modal-confirmation

I've never used this so use at your own risk.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
3
$('confirm text').dialog(
    {
        modal:true, //Not necessary but dims the page background
        buttons:{
            'Save':function() {
                //Whatever code you want to run when the user clicks save goes here
             },
             'Delete':function() {
                 //Code for delete goes here
              }
        }
    }
);

This should work, but you will need to download or link to jQuery and the jQuery UI libraries to run this.

Belinda
  • 1,230
  • 2
  • 14
  • 25