How to Create Confirm Dialog Box in Javascript

Last Updated on April 30, 2024 by Roshan Parihar

To create confirm dialog box in Javascript, use the confirm() and pass the text to display on the box.

It is useful when you want to execute a piece of code after the user confirms that he/she agrees to proceed. If the user clicks on the cancel button of the confirmation box, the given code will not be executed.

What is Javascript COnfirm Dialog Box?

A Javascript confirm dialog box is the alert box that gives warning or informational messages to the users.

The users will get two options on the confirmation box that are ‘OK’ and ‘Cancel’. It proceeds to the next execution of code only after the user clicks the ‘OK’ button. When click ‘Cancel’, it returns false

How to Write Javascript Dialog Box and Get User Confirmation

To get a dialog box for user confirmation, you have to use the confirm() function of Javascript. Also, pass the text content to display on the confirmation box as given below. If the user
click the ‘OK’ button, it returns true otherwise returns false.

Example 1

Output



When you click the button given above, you will get a confirmation box. If you want to proceed with further code execution, you can click the ‘OK’ button.

Get User Input in the Confirmation

If you want to get the user-entered content in the alert box, you can use Javascript prompt(). It requires two arguments as the input where the first argument is the label for the input box and the second is the placeholder of the input box.

The prompt results in an alert box that contains the labels and the input box with the placeholder. See the example given below.

Example 2

Output



Enter the text you want to enter in the input box given in the alert dialog box. After you enter the text in the alert input box, the next alert comes that contains the text you have entered in the prompt input box.