Question

I could only find the function confirm() that gives OK/Cancel buttons. Is there any way to give Yes/No buttons?

Was it helpful?

Solution

Javascript offers 3 modal boxes. prompt, confirm and alert. None of those satisfy your request.

There are a plethora of js modal popup solutions. Here's an example.

OTHER TIPS

No.

Instead you could use a in browser modal popup.

Like everyone else above says, you're stuck with OK/Cancel using confirm().

I would like to recommend this jQuery plugin though: jqModal. I've used it on 3 recent projects and it has worked great for each one. Specifically check out this example:

6). FUN! Overrides -- a. view (alert), b. view (confirm) It is now time to show a real-world use for jqModal -- overriding the standard alert() and confirm dialogs! Note; due to the single threaded nature of javascript, the confirm() function must be passed a callback -- it does NOT return true/false.

No, but there are JavaScript libraries that can accomplish this for you. Just as an example, Ext JS can be used to create a message box dialog.

I'm a fan of jQuery UI Dialog for this sort of thing. Here's a sample...

<script>
  $(function() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height:140,
      modal: true,
      buttons: {
        "Yes": function() {
          $( this ).dialog( "close" );
          alert("You chose Yes!");
        },
        "No": function() {
          $( this ).dialog( "close" );
          alert("You chose No!");
        }
      }
    });
  });
  </script>

<div id="dialog-confirm" title="Are you sure you want to continue?">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

i would use sweetalert https://sweetalert.js.org/guides/ to achieve something like this

swal("Are you sure you want to do this?", {
  buttons: ["yes", "no"],
});
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

Use dialog box to display yes or no

           <div id="dialog_box" class="mnk-modal-bg" style="display:none">
              <div id="dbg" class="mnk-modal-box">
                <i class="uk-icon-exclamation-triangle"  style="color:#757575; padding-right:5px;">
                </i>Confirm?
                <div class="uk-text-center" style="margin-top:10px;">
                    <button class="md-btn md-btn-small md-btn-primary" id="ok_btn">
                        <i class="uk-icon-save" style="padding-right:3px;"></i>OK
                    </button>
                    <button class="md-btn md-btn-small md-btn-danger" id="close_btn">
                        <i class="uk-icon-remove" style="padding-right:3px;"></i>Cancel
                    </button>
                </div>
            </div>

<script>
    $("#ok_btn").click(function(){
        alert("OK");
        $("#dialog_box").hide();
    });
    $("#close_btn").click(function(){
       alert("CANCEL");
       $("#dialog_box").hide();
    });
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top