Making of Confirmation Pop-Up Dialog Box Through JavaScript

Let's have a look at the code which will create a confirmation dialog box with an alert of what users click in the dialog box.
  1. <html>  
  2.   
  3.   <body>  
  4.     <p>Click the button to display a confirmation box.</p>  
  5.     <button onclick="myBox()">Click it</button>  
  6.     <p id="function"></p>  
  7.     <script>  
  8.     function myBox() {  
  9.       var x;  
  10.       var y = confirm("Click a button!");  
  11.       if (y == true) {  
  12.         x = "You clicked OK button!";  
  13.       } else {  
  14.         x = "You clicked Cancel button!";  
  15.       }  
  16.       document.getElementById("function").innerHTML = x;  
  17.     }  
  18.     </script>  
  19.   </body>  
  20.   
  21. </html> 
Output
 
 
After clicking the button the dialog box will be shown.
 
 
When you click the OK or Cancel button the following output will be shown.