Skip to main content

A Message Box with Options

A message box created with the alert() function is used only to display a message to the user and it is equipped with the OK button. In some cases you may want to ask a specific question to the user and expect the user to make a decision. To do this, the JavaScript language allows you to create a message equipped with two buttons, namely OK and Cancel. To display a message box with more than one button, use the confirm() function. Its syntax is:

 
confirm(QuestionToAsk);
 

In the strict sense, the confirm() uses the same argument terminology as the alert() function. This means that you can use it to display a number, a word, a sentence, or a paragraph.

To make it effective, the confirm() function is better used to ask/display a question to the user. The user makes a decision by clicking OK or Cancel. If the user clicks OK, the function returns true. If the user clicks Cancel, the function returns false. Here is an example:

 
<Script Language="JavaScript">
function applicationReminder()
{
  confirm("The application you filled out is not complete.\nDo you still wanto to quit?");
}
</Script>
<input type="button" value="Reminder" onClick="applicationReminder()">
 

Next: Conversion Functions