w3docs logo
  1. Snippets
  2. JavaScript
  3. How to Create a Dialog with JavaScript

How to Create a Dialog with JavaScript

In the scope of this tutorial, we are going to show how you can create a modal dialog with an optional message and “OK” and “Cancel” buttons.

The confirm() JavaScript method will help you to create a dialog. It displays a prompt and returns true or false based on what the user chooses:

w3docs logo Javascript confirm method
if (confirm('Are you sure to save the data?')) { // Save it! console.log('The data was saved.'); } else { // Do nothing! console.log('The data was not saved.'); }

The confirm() method

The Window.confirm() method displays a modal dialog with an optional message and “OK” and “Cancel” buttons. The message is an optional string that is displayed in the dialog, and the result is a boolean, which indicates whether OK (true) or Cancel (false) was selected. If a browser is ignoring in-page dialogs, then the result is always false.

Do you find this helpful?