Create a confirm popup box in JavaScript with “Ok” and “Cancel” option

In this tutorial, we will learn how to create confirm pop-up box in JavaScript.

We can use confirm(parameter string) method with parameter as message. It returns a boolean value (True/False).

It displays messages along with 2 options: OK and CANCEL.

If OK is clicked => True is returned by confirm()

If CANCEL is clicked => False is returned by confirm()

Confirm popup box in JavaScript

Let us see an example,

var opinion=confirm("Do you want to leave this page?");
if(opinion){
 console.log("ok, you can leave.");
}
else{
 console.log("ok, carry on.");
}

Output:

Confirm pop-up box in JavaScript

If ok is clicked=>

ok, you can leave.

else=>

ok, carry on.

Note: Due to browser-provided security, some browsers disabled custom alert boxes so you can not add custom options in all browsers. If browser disable a function you can write that in the comment section.

In this way, we can create a confirm pop-up box in JavaScript.

Also read: Create JavaScript Alert Box With Three Buttons

Leave a Reply

Your email address will not be published. Required fields are marked *