Open In App

Javascript Window confirm() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The confirm() method in JavaScript displays a dialog box with a message and two buttons: OK and Cancel. It is often used to get user confirmation before proceeding with an action, returning true if OK is clicked, and false if Cancel is clicked.

Window confirm() Method Syntax

confirm(message);

Window confirm() Method Parameters

  • message: It is the optional string to be displayed in the dialog. It returns a boolean value indicating whether OK or Cancel was selected (true means OK and false means that the user clicked cancel). 

Window confirm() Method Example

This example shows the working of the window.confirm() function, A dialog box opens with the OK and Cancel button.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <title>Window confirm() Method</title>
</head>
 
<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h2>Window confirm() Method</h2>
    <p>
        Click the button to display a confirm box.
    </p>
    <p id="add"></p>
 
    <button onclick="geek()">
        Click me!
    </button>
 
    <script>
        function geek() {
            let result = confirm("Press OK to close this option");
            if (result === true) {
 
                document.getElementById("add").textContent =
                "User clicked OK";
                console.log("User clicked OK");
            } else {
                document.getElementById("add").textContent =
                "User clicked Cancel";
                console.log("User clicked Cancel");
            }
        }
    </script>
</body>
 
</html>


Output: 

confirm

Explanation:

  • In this example we shows “GeeksforGeeks” and a confirmation button triggering geek() function onclick.
  • geek() function displays a confirmation dialog upon button click.
  • If user clicks “OK”, “User clicked OK” message appears.
  • If “Cancel” clicked, “User clicked Cancel” message appears, along with respective console logs.

JavaScript Window confirm() Method UseCase

1. What is the use of the Confirm Function in JavaScript

The confirm function in JavaScript is used to display a modal dialog box with a message and two buttons: “OK” and “Cancel”. It is commonly used to obtain user confirmation for an action or decision in a web application.

2. How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?

To display a confirmation dialog when clicking an <a> link in JavaScript/jQuery, bind a click event to the link and use window.confirm() to show the dialog.

We have a complete list of HTML DOM methods, to check those please go through the DOM Complete Reference article.

Supported Browsers



Last Updated : 04 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads