offline version v3


239 of 264 menu

confirm function

The confirm function calls up a confirmation box with OK and Cancel buttons. Returns true if the OK button was pressed, and false if the Cancel button was pressed. The parameter takes the text that the user will see.

Syntax

confirm(message);

Example

Let's use the confirm function to display a dialog box. If you press OK in the box that appears, then true will be written to the variable res, and if you press Cancel - then false:

<button id="button">click me</button> let button = document.querySelector('#button'); button.addEventListener('click', function(event) { let res = confirm('Are you 18 years old?'); console.log(res); });

:

See also

  • the alert function
    that pops up a dialog box with a message
  • the prompt function
    that brings up a dialog box with a question
enru