ES6 has three types of dialog boxes. These dialog boxes are used for various purposes as mentioned below:
- Raise an alert that alert function is used to accomplish the task.
- Get confirmation of an event or input - confirm function is used to accomplish the task.
- Get an input from the user -prompt function is used to accomplish the task.
Alert Dialog Box: An alert box is used to give a warning message to the users. Alert box has only one button "OK" to select and continue to the next task. For example, When some input fields are compulsory and the user has not given value to that input field a pop up can be displayed using the alert box.
Syntax:
alert(message);
Example: The following code demonstrates the alert dialog box:
html
<script type="text/javascript">
function display() {
alert("I am an Alert dialog box");
}
</script>
<center>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h4>ES6 Dialog Boxes with alert function</h4>
<p>Click the following button to proceed: </p>
<input type="button" value="Click Me" onclick="display();" />
</center>
Output:
Confirm Dialog Box: A confirm dialog box is used to take the user's confirmation option. It displays a dialog box with two buttons: OK and Cancel. For example, if a user deletes some data, the page can confirm it using a confirm box as if really that data is to be deleted. If the user clicks on the OK button, the method returns true value whereas if the user clicks on the Cancel button, then confirm() method returns false.
Syntax:
confirm(message);
Example: The following code demonstrates the confirm dialog box:
html
<script>
function display() {
var x = confirm ("I am a Confirm dialog box");
if( x == true ) {
document.getElementById("gfg")
.innerHTML = "User wants to continue!";
}
else {
document.getElementById("gfg")
.innerHTML = "User does not want to continue!";
}
}
</script>
<center>
<h1 style="color:green;">GeeksforGeeks</h1>
<h4>ES6 Dialog Boxes with alert function</h4>
<p id="gfg">Click the following button to proceed: </p>
<input type="button" value="Click Me" onclick="display();" />
</center>
Output:
Prompt Dialog Box: The prompt dialog box is used when a text box is to be popped up to get user input. The user needs to give input in the textbox and then click OK. This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, prompt() reads and returns the value entered by the user. If the user clicks the Cancel button, prompt() returns null.
Syntax:
prompt(message, defstring);
Example: The following code demonstrates the prompt dialog box. Here the message is text to be display in the text box and the def string is a default string to display in the text box.
html
<script>
function getValue() {
var retVal = prompt("Enter your Name : ", "name");
document.getElementById("gfg")
.innerHTML = "You have entered : " + retVal;
}
</script>
<center>
<h1 style="color:green;">GeeksforGeeks</h1>
<h4>ES6 Dialog Boxes with prompt function</h4>
<p id="gfg">Click the following button to proceed: </p>
<form>
<input type="button" value="Click Me" onclick="getValue();" />
</form>
</center>
Output:
Supported Browsers: The browsers supported by ES6 Dialog Boxes are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Safari
- Opera
Similar Reads
What is a Dialog Box? A dialog box is nothing but, it is used to request user inputs, provide warnings or notifications, and show message information or options required for user interactions. In this article, we will understand the purpose of the dialog box, examples of the dialog box, and more. What is a Dialog Box?A d
6 min read
Shell Scripting - Dialog Boxes In this article, we will create a shell script that generates a Dialog Box with GUI generating a message to the user. What is a Dialog Box? A Dialog Box is a temporary window an application runs to convey important information to the users. These dialog boxes can be used to display warnings, errors,
7 min read
How to create Dialog Box in ReactJS? In modern web development, creating interactive and user-friendly interfaces is essential. One common element found in many applications is a dialog box. A dialog box is a component that appears on top of the main content to display information, receive user input, or prompt for confirmation. In thi
2 min read
How to create Dialog Box in ReactJS? Dialogs inform users about a task and can contain critical information, require decisions, or involve multiple tasks. Material UI for React has this component available for us and it is very easy to integrate. We can create the dialog box in ReactJS using the following approach:Creating React Applic
2 min read
JQueryUI | dialog The Dialog box is the way to inform the user about something. It is a nice way to popup on the user window to show the information that will happen in the next or any kind of information developer wants to clarify to the user should know. jQueryUI dialog method is used to create a basic dialog windo
2 min read