Open In App

JavaScript Dialogue Boxes

Improve
Improve
Like Article
Like
Save
Share
Report

Dialogue boxes are a kind of popup notification, this kind of informative functionality is used to show success, failure, or any particular/important notification to the user.

JavaScript uses 3 kinds of dialog boxes: 

  • Alert
  • Prompt
  • Confirm

These dialog boxes can be of very much help in making our website look more attractive. 

Alert Box: An alert box is used on the website to show a warning message to the user that they have entered the wrong value other than what is required to fill in that position. Nonetheless, an alert box can still be used for friendlier messages. The alert box gives only one button “OK” to select and proceed.

Example:

JavaScript




<script type="text/javascript">
    function Warning() { 
        alert ("Warning danger you have not filled everything");
        console.log ("Warning danger you have not filled everything"); 
      
    
</script>
  
<p>Click the button to check the Alert Box functionality</p>
<form>
    <input type="button" value="Click Me" onclick="Warning();" />
</form>


Output:

 

Confirm box: A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either “OK” or “Cancel” to proceed. If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false and will show null.

Example:

JavaScript




<script type="text/javascript">
    function Confirmation() {
        var Val = confirm("Do you want to continue ?");
        if (Val == true) {
            console.log(" CONTINUED!");
            return true;
        } else {
            console.log("NOT CONTINUED!");
            return false;
        }
    }
</script>
  
<p>Click the button to check the Confirm Box functionality</p>
<form>
    <input type="button" value="Click Me" onclick="Confirmation();" />
</form>


Output:

 

Prompt Box: A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. If the user clicks the OK button, the window method prompt() will return the entered value from the text box. If the user clicks the Cancel button, the window method prompt() returns null.

Example:

JavaScript




<script type="text/javascript">
    function Value(){
    var Val = prompt("Enter your name : ", "Please enter your name");
    console.log("You entered : " + Val);
    }
      
</script>
  
<p>Click the button to check the Prompt Box functionality</p>
<form>
    <input type="button" value="Click Me" onclick="Value();" />
</form>


Output:

 

Line Breaker: If you want a break in your dialogue box message, you can put a line breaker(“\n”) there.

Example:

JavaScript




<p>Line-Breaks in Dialogue box(Alert function)</p>
<button onclick="alert('Geeksfor\nGeeks')">
  CLICK ME
</button>


Output:

 

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Last Updated : 07 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads