Reputation: 54793
I could only find the function confirm()
that gives OK/Cancel buttons. Is there any way to give Yes/No buttons?
Upvotes: 22
Views: 20512
Reputation: 2702
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.
Upvotes: 7
Reputation: 2366
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>
Upvotes: 2
Reputation: 165
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>
Upvotes: 0
Reputation: 32704
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>
Upvotes: 1
Reputation: 36473
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.
Upvotes: 2
Reputation: 1122
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.
Upvotes: 1