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

  • Void keywords in JavaScriptPhoto of Void 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.
  • Print pages in JavaScriptPhoto of Print pages in JavaScript
    many times you will love to put a button on your website to print the content of that page through a printer. javascript helps you perform this function by using the print function of the window object.
  • Objects in JavaScriptPhoto of Objects in JavaScript
    javascript is an object oriented programming language (object oriented programming). a program language can be called object-oriented if it provides four basic capabilities to the programmer.
  • Tutorial for creating slideshows in JavaScript with 3 easy stepsPhoto of Tutorial for creating slideshows in JavaScript with 3 easy steps
    if you are studying or interested in programming, do not skip the article below to guide how to create slideshows in java script with 3 simple steps.
  • 7 Framework JavaScript for mobile application developmentPhoto of 7 Framework JavaScript for mobile application development
    if you want to start creating mobile applications, there are three main languages ​​to choose from: java for android, swift applications for ios or javascript applications for platform applications. in this article, you will learn about seven javascript frameworks equipped to create mobile applications.
  • Introduction to 2D Array - 2-dimensional array in JavaScriptPhoto of Introduction to 2D Array - 2-dimensional array in JavaScript
    in the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.