Dialogs - Alert, Prompt, Confirmation in JavaScript

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

Message dialog box (Alert)

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

However, a message 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 results

Confirmation dialog box

A Confirmation Dialog is most commonly used to receive user consent on any of the options. It displays a dialog box with two buttons: OK and Cancel .

If the user clicks the OK button, the confirm () method will return true. If the user clicks the Cancel button, 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 results

Suggested dialog box (Prompt)

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

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

This dialog box has two buttons: OK and Cancel . If the user clicks the OK button, the prompt () method will return the value entered from the text box. If the user presses the Cancel button, the prompt () method returns 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 results

According to Tutorialspoint

Previous post: Page navigation (Redirect) in JavaScript

Next article: Void keyword in JavaScript

4 ★ | 1 Vote

May be interested

  • ! = and! == What is the difference in JavaScript?! = and! == What is the difference in JavaScript?
    javascript includes operators like in other languages. an operator performs some operations on one or more operands (data values) and produces a result. today's article will help readers learn about 2! = and! == operators in javascript.
  • How to turn off the delete confirmation dialog in OutlookHow to turn off the delete confirmation dialog in Outlook
    the confirmation message deletion dialog box in outlook will always appear even if the user has e-mailed the trash, or set up the email delivery rules.
  • Learn about ES6 in JavascriptLearn about ES6 in Javascript
    es6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
  • Summary of JavaScript exercises with sample codeSummary of JavaScript exercises with sample code
    in order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
  • JavaScript location in HTML FileJavaScript location in HTML File
    there is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
  • How to Turn on JavaScriptHow to Turn on JavaScript
    javascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
  • What is the difference between Java and JavaScript?What is the difference between Java and JavaScript?
    although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages ​​are not technically related to each other.
  • Void keywords in JavaScriptVoid keywords in JavaScript
    void is an important keyword in javascript that can be used as a unary operator before its single operand, which can be in any type. this operator defines an expression to be evaluated without returning a value.
  • JavaScript code to generate error charts & graphsJavaScript code to generate error charts & graphs
    the example below illustrates a sample variance chart created with javascript that incorporates a column chart. you will also get the source code for reference and editing as you wish.
  • How to use the command history function in Command PromptHow to use the command history function in Command Prompt
    command prompt is an extremely familiar command for anyone using windows operating system. besides, a lot of current software also supports the command line to perform actions on the command prompt window, instead of on the screen.