ArticleZip > How To Show Confirmation Alert With Three Buttons Yes No And Cancel As It Shows In Ms Word

How To Show Confirmation Alert With Three Buttons Yes No And Cancel As It Shows In Ms Word

Imagine you're working on an app or website and need to create a confirmation alert with three buttons – Yes, No, and Cancel, just like you see in Microsoft Word. In this article, I'll guide you through the process of incorporating this essential feature into your own projects using code snippets that you can easily adapt to suit your needs.

To begin, let's use JavaScript to achieve this functionality. We'll first create a function that will display the confirmation alert with the required buttons. Here's how you can implement it:

Javascript

function showConfirmationAlert() {
  var result = confirm("Do you want to save changes?");
  
  if (result) {
    // User clicked 'Yes' button
    console.log("Changes saved successfully.");
  } else if (result === false) {
    // User clicked 'No' button
    console.log("Changes not saved.");
  } else {
    // User clicked 'Cancel' button or closed the alert
    console.log("Operation canceled.");
  }
}

In the above code snippet, the `confirm` function is used to create a confirmation dialog that returns `true` if the user clicks 'Yes,' `false` if the user clicks 'No,' and `null` if the user clicks 'Cancel' or closes the alert without clicking any button.

To trigger this function in your project, you can call it based on user interactions such as a button click event. For example:

Javascript

document.getElementById("saveButton").addEventListener("click", showConfirmationAlert);

In the code above, we're attaching an event listener to a button with the id "saveButton." When this button is clicked, the `showConfirmationAlert` function will be executed, displaying the confirmation alert with the desired buttons.

Customizing the confirmation alert further is simple. You can modify the text displayed in the dialog box to suit your specific requirements by changing the message passed to the `confirm` function.

Remember, user experience is crucial when incorporating alerts into your applications. Ensure that the text and button options are clear and concise to prevent any confusion.

By following these steps and understanding how JavaScript's `confirm` function works, you can easily implement a confirmation alert with three buttons – Yes, No, and Cancel – reminiscent of the familiar dialog in Microsoft Word. Experiment with different messages and behaviors to create a seamless user experience in your projects.

×