Advertisement
Advertisement


Code to ask yes/no question in javascript


Question

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

2008/08/21
1
20
8/21/2008 3:20:05 AM

Accepted Answer

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.

2019/11/20
8
11/20/2019 10:19:09 AM

No.

Instead you could use a in browser modal popup.

2008/08/21

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.

2008/08/21

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.

2008/08/21

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>
2014/04/07

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>

2018/01/07

Source: https://stackoverflow.com/questions/19280
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]