Conform Box in JavaScript

1. Purpose
  • Displays a dialog box with a message and two buttons: “OK” and “Cancel”.1
  • Used to get user confirmation before performing an action.
2. Syntax
let confirmation = window.confirm("Are you sure you want to proceed?");

Explanation:

  • window.confirm(“Your Message Here”); This method displays a dialog box with the specified message.
  • Returns:
    • true if the user clicks the “OK” button.
    • false if the user clicks the “Cancel” button.
3. Example
<!DOCTYPE html>
<html>
<head>
<title>Confirm Box Example</title>
</head>
<body>
<button onclick="confirmAction()">Click Me</button>
<script>
function confirmAction() {
if (confirm("Are you sure you want to proceed?")) {
alert("You clicked OK!");
} else {
alert("You clicked Cancel!");
}
}
</script>
</body>
</html>

In this example:

  • When the button is clicked, the confirmAction() function is called.
  • confirm(“Are you sure you want to proceed?”) displays the confirmation dialog.
  • The if statement checks the user’s response:
    • If the user clicks “OK”, the first alert() is displayed.
    • If the user clicks “Cancel”, the second alert() is displayed.
Scroll to Top