Table of contents
1.
Introduction
2.
Getting Started
2.1.
Alert Dialog Box
2.2.
Prompt Dialog Box
2.3.
2.4.
Confirm Dialog Box
3.
FAQs
4.
Key takeaways:
Last Updated: Mar 27, 2024

Dialog Boxes in Javascript

Author GAZAL ARORA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript supports dialog boxes. These dialog boxes or popups are used to interact with the user. They are also used to notify a user or to receive some user input before proceeding. 

 

There are three types of dialog boxes:

  1. Alert Dialog Box
  2. Prompt Dialog Box
  3. Confirm Dialog Box

Read More About, Basics of Javascript

 

Let's explore them one by one with the help of examples.

Getting Started

 

Alert Ninjas! The ongoing emergence of new technologies in web development keeps the interest in this subject high. But, before you start on the big tasks, we recommend that you understand the fundamentals. With our JavaScript Course, you may jumpstart your web development career by mastering JS principles. It's now the cheapest it's ever been!

 

These dialog boxes' appearance is defined by the operating system or browser settings. They cannot be changed using CSS. Also, dialog boxes are modal windows, which means that when one is presented, code execution stops and only begins when the dialog box is dismissed.

 

We'll go through each of these dialog boxes in-depth in the following section.

Alert Dialog Box

An alert dialog box is typically used to provide users with a warning message. For example, if one input field requires text input but does not provide any, use an alert box to display a warning message.

 

Example:

 

<!DOCTYPE html>
<html>
  <head>
        
      <script>
        <!--
            function Warning() {
              alert ("You haven’t filled everything");
              document.write ("You haven’t filled everything");
            }
      </script>

 
  </head>
  <body>
      
<p>Click here for results </p>
      
      <form>
        <input type="button" value="Click"

          onclick="Warning();" />
      </form>
      
  </body>
</html>

 

When you will run this code,

 

Output:

 

On clicking this “Click” button, you will get a pop-up warning box like this:

 

For good practice, you can try it on an online javascript editor.

 

Prompt Dialog Box

If you want the user to enter a value before proceeding to the next page, a prompt box is a solution.

The prompt() method is used to display this dialog box, and it accepts two parameters: (i) a label to display in the text box and (ii) a default string to display in the text box.

 

Example:

 

<!DOCTYPE html>
<html>
  <head>
  
      <script>     
            function Value(){
              var Val = prompt("Enter name : ""name");
              document.write("You entered : " + Val);
            }
  
      </script>   

  
  </head>  
  <body>
      
      <p>Click here for results: </p>  
      <form>
        <input type="button" value="Click"          onclick="Value();" />
      </form>
      
  </body>
</html>

 

When you will run this code,

 

Output:

 

On clicking this “Click” button, you will get a textbox like this:

 

There are two buttons in this dialogue box: OK and Cancel. The window method prompt() will return the entered value from the text box if the OK button is pressed or returns null if the user hits the Cancel button. The prompt() method always returns a string value. 

Confirm Dialog Box

Confirm Dialog Box allows users to confirm or approve anything. To proceed, the user must click "OK" or "Cancel" when a confirmation window appears. It will return true if the user hits the OK button. If the user presses the Cancel button, confirm() returns false, and null is displayed.

 

Example:

 

<!DOCTYPE html>
<html>
  <head>
    
      <script>
            function Confirmation(){
              var value = confirm("Do you want to continue? ");
              if( value == false ){
                  document.write (" Not Continued!");
                  return false;
              }
              else{
                  document.write ("Continued!");
                  return true;
              }
            }
      </script>
      
  </head>
  <body>
      
      <p>Click here for results: </p>   
      <form>
        <input type="button" value="Click"  onclick="Confirmation();" />
      </form>     
  </body>
</html>

When you will run this code,

 

Output:

 

 

On clicking this “Click” button, you will get a confirmation box like this:

 

 


 

 

Tip: A newline character or line feed (\n) displays line breaks inside the dialog boxes.

 



Must Read Difference Between "var", "let" and "const" in JS

FAQs

  1. What do you mean by dialog box?
    A dialog box is a typical window in an operating system's graphical user interface. The dialogue box provides additional information and requests input from the user.
     
  2. What are the dialog boxes in JavaScript?
    JavaScript provides three kinds of dialog boxes: ALERT, PROMPT, and CONFIRM.
     
  3. What is the shortcut to open the dialog box in Word?
    Use Ctrl+F12 to open the Open dialog box directly.

 

Key takeaways:

Today we learned about three dialog boxes provided by Javascript:

  1. Alert dialog box: Use to give a warning to the user.
  2. Prompt dialog box: Use to get some input value from the user.
  3. Confirm dialog box: Used to take confirmation from the user before moving ahead.

Use these dialog boxes to build some amazing websites. Coding Ninjas provides a whole course on NodeJS. It offers basic to Advance level understanding. Enroll today and build unique websites using Node. Click on the given link to enroll. With the help of our Javascript guided path, you can start pracitising your js skills.

 

Live masterclass