You’re probably looking for confirm(), which displays a prompt and returns true or false based on what the user decided:

if (confirm('Are you sure you want to save this thing into the database?')) {
  // Save it!
  console.log('Thing was saved to the database.');
} else {
  // Do nothing!
  console.log('Thing was not saved to the database.');
}

Answer from s4y on Stack Overflow
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Confirm Yes or No With JavaScript | Envato Tuts+ - Code
July 21, 2021 - It also cannot be customized, for example if you want to say Yes or No instead of OK and Cancel. Finally, the confirmation dialog is modal, so as long as it is being displayed, the user will not be able to interact with any other part of your app's interface. Another way to confirm yes or no is with a hidden div on your page.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Window › confirm
Window: confirm() method - Web APIs - MDN Web Docs
const windowButton = document.querySelector("#windowButton"); const log = document.querySelector("#log"); windowButton.addEventListener("click", () => { if (window.confirm("Do you want to open in new tab?")) { window.open("https://developer.mozilla.org/en-US/docs/Web/API/Window/open"); } else { log.innerText = "Glad you're staying!"; } }); Dialog boxes are modal windows — they prevent the user from accessing the rest of the program's interface until the dialog box is closed. For this reason, you should not overuse any function that creates a dialog box or a modal window.
🌐
PHPpot
phppot.com › javascript › javascript-confirm
JavaScript Confirm Dialog Box with Yes No Alert - PHPpot
The Javascript confirm() function accepts an optional message to be shown on the consent box. It also displays the OK and ‘Cancel’ buttons to allow users to say yes or no about the confirmation consent. This function returns a boolean true or false based on the button clicks on the confirm dialog box.
🌐
W3Schools
w3schools.com › jsref › met_win_confirm.asp
Window confirm() Method
HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference AngularJS Reference jQuery Reference
🌐
Coderanch
coderanch.com › t › 528581 › languages › change-buttons-confirm-alert-box
Can I change the buttons in a confirm alert box to 'Yes' , 'No'? (HTML Pages with CSS and JavaScript forum at Coderanch)
I have a requirement that requires a confirm box with 'yes' 'No' buttons. But the default confirm box shows just 'OK', 'Cancel'. How can i customize them? Im using javascript and Java. Anyone who has experience in this, please guide me. sample code would help. ... You can not change the confirm, alert, or prompt. You would have to build your own out of html and display it. Look at pre-built stuff like http://jqueryui.com/demos/dialog/#modal-confirmation
Find elsewhere
🌐
Daypilot
code.daypilot.org › 67418 › javascript-confirm-replacement
JavaScript confirm() Replacement | DayPilot Code
Familiar API very similar to the original confirm() function · Custom CSS themes are supported · You can replace OK/Cancel buttons with Yes/No or other buttons · The question displayed by the modal dialog can be defined using text or custom HTML · The method returns a promise, so you can use async/await syntax to simplify the code · See also: JavaScript alert() Replacement ·
🌐
Quora
quora.com › How-do-you-create-a-dialog-with-yes-and-no-options-in-JavaScript
How to create a dialog with “yes” and “no” options in JavaScript - Quora
Answer (1 of 2): In the future, I would strongly recommend Googling questions like this prior to asking. The answer is readily available. You want to use the confirm method. You pass in the message you would like to display in the dialog as the argument like the following [code ]confirm(‘Click ...
🌐
sebhastian
sebhastian.com › javascript-confirmation-yes-no
JavaScript - Create confirmation box with yes and no options | sebhastian
July 6, 2022 - 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.
🌐
LearnersBucket
learnersbucket.com › home › examples › es6 › javascript confirm box with yes & no option
Javascript confirm box with yes & no option - LearnersBucket
April 8, 2019 - If yes or no button is pressed inside the confirm box then pass true or false to javascript callback function and hide the modal. function confirmDialog(message, handler){ $(`<div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div ...
🌐
ProgrammingBasic
programmingbasic.com › home › javascript › create confirmation alert box with yes and no options - javascript
Create Confirmation Alert Box with YES and NO options - JavaScript | ProgrammingBasic
So we cannot change the OK and Cancel buttons on the confirmation dialog box. So we have to code our HTML confirmation box with Yes and No buttons and create our own function which will check if a user has clicked the yes button or the No button.
🌐
EyeHunts
tutorial.eyehunts.com › home › confirm box in javascript with yes no option
Confirm box in JavaScript with yes no option
March 26, 2024 - <!DOCTYPE html> <html> <body> <script> ...elete</button> </body> </html> ... To create a dialog with “yes” or “nor”, use a custom dialog box....
Top answer
1 of 3
5

The buttons in a confirmation box cannot be changed by Javascript.

I would suggest rolling your own in the form of an inline popup. Just create a div with position:absolute; in the centre of the page and then show/hide it.

EDIT:

The code below will outline what you need to do in vanilla Javascript. You will probably want to spend more time styling it, but the key points are:

position:absolute; So that the popup will appear in the centre of the page. display:none; So that the popup will be hidden when the page loads. The link has a href so that it will still be functional even without Javascript. The onClick attribute of the first link has return false; This stops the link from redirecting.

You can change the two onClicks inside the popup to do whatever else you want them to.

Copy<style type="text/css">

div#popup
{
    position:absolute;
    display:none;
    top:200px;
    left:50%;
    width:500px; 
    margin-left:-250px;
    border:1px solid blue; 
    padding:20px;
    background-color:white;
}

</style>

<a 
    href="http://example.com/" 
    onclick="document.getElementById('popup').style.display = 'block'; return false;"
>Go to example.com</a>


<div id="popup">
    <p>Are you sure you want to go to example.com?</p>
    <p>
        <a onclick="document.location='http://example.com/'; return false;">
            Yes
        </a>
        <a onclick="document.getElementById('popup').style.display = 'none'; return false;">
            No
        </a>
    </p>
</div>

To get a more professional result I would recommend learning more about JavaScript and jQuery and investigating some of the options suggested by the other posters.

2 of 3
1

The basics are:

  1. Create a transparent (or semi-transparent) iframe to cover the browser viewport, which does a couple of things for you:
    • Eats clicks outside your confirmation box
    • Prevents OS-drawn controls (like select boxes) from appearing on top of your confirmation box (which they'll do otherwise, on IE at least and possibly others)
    • And lets you (optionally) shade the rest of the page to highlight your confirmation box. Give the iframe a z-index (100, say, unless you have other elements on the page with a higher z-index than that).
  2. Create a div that contains your yes/no question and buttons, append it to your main page's DOM, position it where you want it, and give the div a z-index greater than that of the iframe. Believe it or not, this means that the page is behind the iframe, but the div is in front of it. Exactly what you want.
  3. Handle clicks on the buttons to tear the whole thing down (and to get your answer).
  4. Remember that this will not be inline with your JavaScript logic. You use callbacks from the buttons instead.

It really is that easy (or that complicated). :-)

Having said that, this wheel has been invented. Look for "lightbox" or similar components. jQuery UI has one called Dialog, for instance, but just adding that to a page where you're not using jQuery UI for anything else may be a bit heavy.

🌐
Sabe
sabe.io › blog › javascript-yes-no-confirmation-box
How to Create a Yes/No Confirmation Box in JavaScript | Sabe
February 12, 2022 - The best way to create a yes/no confirmation box is to use the JavaScript confirm() function. This function will make the browser render a dialog box with a message and two buttons, an Ok and a Cancel button.
🌐
TutorialsPoint
tutorialspoint.com › How-to-create-a-dialog-with-yes-and-no-options-in-JavaScript
How to create a dialog with “yes” and “no” options in JavaScript?
July 30, 2019 - To create a dialog with “yes” or “nor”, use a custom dialog box. ... <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> function functionConfirm(msg, myYes, myNo) { var confirmBox = $("#confirm"); ...
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-make-a-custom-yes-no-alert-in-javascript-6cd2de221e6c
How to make a custom ‘yes/no’ alert in JavaScript? | by Abhi | JavaScript in Plain English
September 26, 2020 - For example- if you make a confirm alert box in JavaScript, it’d make a confirmation box with an OK or Cancel box. Now suppose you are making a quiz or some sort of web application, and you want to make a JavaScript alert with a Yes or No box instead of Ok or Cancel box, how are you going to do it? Sadly, you can’t. though you can make custom alert boxes within the webpage with HTML, CSS, and JavaScript.