Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript confirm box with custom buttons

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

like image 318
Hector Barbossa Avatar asked Oct 20 '10 09:10

Hector Barbossa


People also ask

How do I change the button text in confirm box?

JavaScript users can create the confirm box using the . confirm() method, which contains the confirmation message string, ok, and cancel button. The programmer can't change the confirm box style and button label if they use the default confirm box.

How can write confirm box in JavaScript?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.

How many buttons are there in Confirm box?

A Confirmation Box is used to provide user with a choice about an event. This type of popup box has two buttons, named 'OK' and 'Cancel', and return 'true' and 'false' when respective buttons are clicked.

How do you use confirm in JavaScript?

Javascript | Window confirm() Method The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed.


2 Answers

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.

like image 124
Tim Down Avatar answered Oct 04 '22 00:10

Tim Down


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>
like image 39
Tim Schmelter Avatar answered Oct 03 '22 23:10

Tim Schmelter