Dialog - Alert, Prompt, Confirmation in JavaScript
IT Tricks

Dialog – Alert, Prompt, Confirmation in JavaScript


JavaScript supports 3 important dialog types. These dialog boxes can be used to notify, confirm input, or receive input from users. Below we discuss each type of dialog box.

Message dialog box (Alert)

An alert dialog box (Alert Dialog Box) is most commonly used to alert users. For example, if an input field requires some text but the user does not provide any input, then as part of the confirmation, you can use the message box to give a scene. newspaper.

However, a notification box may still be used for more friendly notifications. The message box only provides an OK button to select and continue.

For example

   type="text/javascript">     

Click the following button to see the result:

type="button" value="Click Me" onclick="Warn();" />

Result

Follow the above method to see the result

Confirmation dialog box (Confirmation)

A Confirmation Dialog is most commonly used to receive user consent on any option. It displays a dialog box with two buttons: OK, got it and Cancel.

If the user clicks the button OK, got it, method confirm () will return true. If the user clicks the button Cancel, then confirm () returns false. You can use a confirmation dialog as follows:

For example

   type="text/javascript">     

Click the following button to see the result:

type="button" value="Click Me" onclick="getConfirmation();" />

Result

Follow the above method to see the result

Suggestions dialog box (Prompt)

The Prompt Dialog Box is useful when you want to pop-up a text box to receive user input. Therefore, it gives you the ability to interact with users. The user needs to fill in that field and then click OK.

This dialog is displayed by using a method of prompt () which takes two parameters: (i) A label you want to display in the text box and (ii) A default string to display in the text box.

This dialog has two buttons: OK, got it and Cancel. If the user clicks the OK button, the method prompt () will return the value entered from the text box. If the user presses the Cancel button, the method prompt () return null.

For example

The following is an example of how to use a Prompt dialog box.

   type="text/javascript">     

Click the following button to see the result:

type="button" value="Click Me" onclick="getValue();" />

Result

Follow the above method to see the result

Follow Tutorialspoint

Previous lesson: Page Navigation (Redirect) in JavaScript

Next article: Void keyword in JavaScript

.

Leave a Reply

Your email address will not be published. Required fields are marked *