Custom Confirmation Modal in JavaScript Tutorial

This tutorial tackles about Creating a Custom Confirmation Modal in JavaScript. This is a dynamic confirmation message which allows your user to confirm first before executing the JS function. This can be used for some action or feature in your current or future web application projects such as delete operations. The confirmation feature before executing an action in a web application can help your user to prevent unintentional execution of the action.

Here, I will be providing a simple application that explains the main goal of this tutorial. In the application, the confirmation has 3 options which are execute function, execute the function with a single parameter, and execute a function with multiple paramater. Before we proceed to the coding part, please make sure that your device is connected to the internet due the source code uses CDNs for the external libraries.

Lets do the coding...

Create Main Interface

In your preferred text editor, create a new HTML file naming index.html. This file contains the script of the elements which outputs a simple page with 4 different buttons and modal element. Since the we are creating a dynamic confirmationa modal, we'll be only needing 1 confirmation modal container.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Custom Confirmation Modal</title>
  8.     <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
  9.     <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  10.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
  11.     <script src="./script.js"></script>
  12. </head>
  13.  
  14.     <div class="container py-5">
  15.         <h3 class="text-center">Custom Confimation Modal</h3>
  16.         <hr>
  17.         <div class="text-center">
  18.             <div class="row row-cols-xl-6 row-cols-md-4 row-col-xs-2 justify-content-center gx-2 gy-3">
  19.                 <button class="btn btn-primary bg-gradient-primary mx-2 my-3" type="button" id="action1">Action 1</button>
  20.                 <button class="btn btn-light bg-gradient-light border mx-2 my-3" type="button" id="action2">Action 2</button>
  21.                 <button class="btn btn-info bg-gradient-info mx-2 my-3" type="button" id="action3">Action with Single Parameter</button>
  22.                 <button class="btn btn-warning bg-gradient-warning mx-2 my-3" type="button" id="action4">Action with Multiple Parameter</button>
  23.             </div>
  24.         </div>
  25.     </div>
  26.     <div class="modal fade" id="confirm_modal" tabindex="-1">
  27.         <div class="modal-dialog modal-dialog-centered rounded-0">
  28.             <div class="modal-content rounded-0">
  29.                 <div class="modal-header py-1">
  30.                     <h5 class="modal-title">Confirmation</h5>
  31.                 </div>
  32.                 <div class="modal-body">
  33.                 </div>
  34.                 <div class="modal-footer py-1">
  35.                     <button type="button" class="btn btn-primary btn-sm rounded-0 py-1" id="confirm-btn">Confirm</button>
  36.                     <button type="button" class="btn btn-secondary btn-sm rounded-0 py-1" data-bs-dismiss="modal">Cancel</button>
  37.                 </div>
  38.             </div>
  39.         </div>
  40.     </div>
  41.     <div class="modal fade" id="action_display" tabindex="-1">
  42.         <div class="modal-dialog modal-dialog-centered rounded-0">
  43.             <div class="modal-content rounded-0">
  44.                 <div class="modal-header py-1">
  45.                     <h5 class="modal-title">Action Result</h5>
  46.                 </div>
  47.                 <div class="modal-body">
  48.                 </div>
  49.                 <div class="modal-footer py-1">
  50.                     <button type="button" class="btn btn-secondary btn-sm rounded-0 py-1" data-bs-dismiss="modal">Cancel</button>
  51.                 </div>
  52.             </div>
  53.         </div>
  54.     </div>
  55. </body>
  56. </html>

Creating the Main Action

Below is the JavaScript code that contains the scripts that makes our goal in this tutotial possible. Create a new file naming script.js and copy/paste the source code below.

  1. function action1() {
  2.     var result = "Action 1 has been clicked";
  3.     var action_display = $('#action_display')
  4.     action_display.find('.modal-body').html(result)
  5.     action_display.modal('show')
  6. }
  7.  
  8. function action2() {
  9.     var result = "Action 2 has been clicked";
  10.     var action_display = $('#action_display')
  11.     action_display.find('.modal-body').html(result)
  12.     action_display.modal('show')
  13. }
  14.  
  15. function action3($param1 = '') {
  16.     var result = "Action 3 has been clicked. Parameter = " + $param1;
  17.     var action_display = $('#action_display')
  18.     action_display.find('.modal-body').html(result)
  19.     action_display.modal('show')
  20.  
  21. }
  22.  
  23. function action4($param1 = '', $param2 = '') {
  24.     var result = "Action 1 has been clicked. parameter 1 = \'" + $param1 + "\', parameter 2 = \'" + $param2 + "\'";
  25.     var action_display = $('#action_display')
  26.     action_display.find('.modal-body').html(result)
  27.     action_display.modal('show')
  28.  
  29. }
  30. window._confirm = function($message = '', $func = '', $param = []) {
  31.     if ($func != '' && typeof window[$func] == 'function') {
  32.  
  33.         var modal_el = $('#confirm_modal')
  34.         modal_el.find('.modal-body').html($message)
  35.         modal_el.modal('show')
  36.         modal_el.find('#confirm-btn').click(function(e) {
  37.             e.preventDefault()
  38.             if ($param.length > 0 && !!$.isArray($param))
  39.                 window[$func].apply(this, $param)
  40.             else
  41.                 window[$func]($param)
  42.             modal_el.modal('hide')
  43.         })
  44.     } else {
  45.         alert("Function does not exists.")
  46.     }
  47. }
  48. $(function() {
  49.     $("#action1").click(function() {
  50.         _confirm("Are you sure to continue to execute action 1?", 'action1')
  51.     })
  52.     $("#action2").click(function() {
  53.         _confirm("Are you sure to continue to execute action 2?", 'action2')
  54.     })
  55.     $("#action3").click(function() {
  56.         _confirm("Are you sure to continue to execute action 3?", 'action3', 'Single Parameter')
  57.     })
  58.     $("#action4").click(function() {
  59.         _confirm("Are you sure to continue to execute action 4?", 'action4', ['Hello World!', "SourceCodester"])
  60.     })
  61. })

As you can see at the script above, window._confirm() function is the script for executing our custom confirmation modal. It has 3 parameters which are first the message, next the function name to be executed when confirmed, and lastly is the function parameter. The function parameter is optional, which can be a string or an integer. But for sending multiple parameters to the function, please ensure that your 3rd parameter for _confirm() function is an Array.

That's it! You can now test the application and see if it achieves our goal for this tutorial. If there's an error occurred on your end, please review your source code with my provided source code scripts above. You may also download the working source code file that I created for this tutorial by clicking the download button below.

I hope this Custom Confirmation Modal in JS Tutorial helps you with what you are looking for and useful for your future projects. Explore more on this website for more Tutorals and Free Source Codes.

Happy Coding :)

Add new comment