JavaScript confirm box is similar to alert message box that we learnt in JavaScript Alert Example article. In JavaScript, Confirm box is used to confirm the event action performed by the user interacting with the web page form inputs. E.g.: A user is trying to delete any item from his account then you can trigger a confirm box for that event to ask the user whether he is sure to delete that item or action. Confirm box displays 2 action buttons when it pops up at any event, it shows OK and CANCEL button. You can detect the button pressed by the user and can perform the required action when OK button is clicked by the user.
Syntax of JavaScript Confirm Box
confirm([message]);
Example of confirm box in JavaScript
function confirmMessage() {
var res = confirm('Are you sure?');
if (res == true) {
document.write('You clicked the OK button');
}
else {
document.write('You clicked the CANCEL button');
}
}
Above example shows that confirm box result is stored into variable res, then it is detected which button is being clicked by the user.
Continueto next tutorial: Javascript Prompt Box to learn how to open the prompt box using JavaScript prompt method.