Skip to main content

Creating a Dynamic Confirmation Dialog using jQuery and Bootstrap Modal Tutorial

Screenshot
Body

In this tutorial, I will teach/share with you some techniques that I am using when developing a web application. This tutorial will tackle about Creating a Dynamic Confirmation Dialog using jQuery and Bootstrap Modal. This kind of web application feature can help you to reduce the number of lines you are writing and also to prevent redundancy in creating a function in your source code.

To give you a better understanding of this tutorial, I will be providing a simple web application that has dummy table data. The application contains a delete table row function and an emptying table function. The application that we will create has only static data which means there's no database server needed for this.

Getting Started

In this tutorial, I used Bootstrap v5 for the design of application. And, kindly download also the jQuery Lirary. After that, compile you CSS and Script assets into a directory.

Creating the Page Interface

The script below is the HTML code/script for the index page of the application that we will be creating. This contains navbar, table, static data, buttons, and the confirmation modal HTML scripts.

Open a text editor i.e. (sublime, notepad++, or vs code editor) and create a new HTML file naming index.html and save the file inside your created source code directory.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Confirmation Modal/Dialog using PHP</title>
  9. <link rel="stylesheet" href="./css/bootstrap.min.css">
  10. <script src="./js/jquery-3.6.0.min.js"></script>
  11. <script src="./js/bootstrap.min.js"></script>
  12. <script src="./js/script.js"></script>
  13. <!-- Custom CSS -->
  14.  
  15. </style>
  16. </head>
  17.  
  18. <body class="bg-light">
  19. <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
  20. <div class="container">
  21. <a class="navbar-brand" href="https://sourcecodester.com">
  22. Sourcecodester
  23. </a>
  24. </div>
  25. </nav>
  26. <div class="container py-3" id="page-container">
  27. <small>How to create a dynamic confirmation dialog or modal using jQuery</small>
  28. <hr>
  29. <div id="printables">
  30. <div class="to_print" id="first_element">
  31. <h3><b>Dummy Data</b></h3>
  32. <hr>
  33. <button class="btn btn-sm btn-success float-end rounded-0 my-2" type="button" id="empty-table">Empty Table</button>
  34. <table class="table table-bordered table-striped" id="dummy-tbl">
  35. <tr class="bg-primary bg-gradient text-white">
  36. <th class="text-capitalize text-center py-1 px-2">name</th>
  37. <th class="text-capitalize text-center py-1 px-2">phone</th>
  38. <th class="text-capitalize text-center py-1 px-2">email</th>
  39. <th class="text-capitalize text-center py-1 px-2">address</th>
  40. <th class="text-capitalize text-center py-1 px-2">region</th>
  41. <th class="text-capitalize text-center py-1 px-2">text</th>
  42. <th class="text-capitalize text-center py-1 px-2">action</th>
  43. </tr>
  44. </thead>
  45. <tr data-id="1">
  46. <td class="px-2 py-1">Caldwell Stewart</td>
  47. <td class="px-2 py-1">(723) 398-8511</td>
  48. <td class="px-2 py-1">[email protected]</td>
  49. <td class="px-2 py-1">Ap #499-3016 Tincidunt St.</td>
  50. <td class="px-2 py-1">Antwerpen</td>
  51. <td class="px-2 py-1">Mauris vestibulum, neque sed dictum eleifend, nunc risus varius orci,</td>
  52. <td class="px-2 py-1 text-center align-middle">
  53. <button class="btn btn-danger btn-sm rounded-0 delete-data" type="button">Delete</button>
  54. </td>
  55. </tr>
  56. <tr data-id="2">
  57. <td class="px-2 py-1">Jesse Serrano</td>
  58. <td class="px-2 py-1">(426) 346-7475</td>
  59. <td class="px-2 py-1">[email protected]</td>
  60. <td class="px-2 py-1">Ap #197-2368 Netus Rd.</td>
  61. <td class="px-2 py-1">Nordrhein-Westphalen</td>
  62. <td class="px-2 py-1">enim consequat purus. Maecenas libero est, congue a, aliquet vel,</td>
  63. <td class="px-2 py-1 text-center align-middle">
  64. <button class="btn btn-danger btn-sm rounded-0 delete-data" type="button">Delete</button>
  65. </td>
  66. </tr>
  67. <tr data-id="3">
  68. <td class="px-2 py-1">Shea Donovan</td>
  69. <td class="px-2 py-1">1-512-856-0720</td>
  70. <td class="px-2 py-1">[email protected]</td>
  71. <td class="px-2 py-1">429-7540 Nulla. Avenue</td>
  72. <td class="px-2 py-1">British Columbia</td>
  73. <td class="px-2 py-1">lectus rutrum urna, nec luctus felis purus ac tellus. Suspendisse</td>
  74. <td class="px-2 py-1 text-center align-middle">
  75. <button class="btn btn-danger btn-sm rounded-0 delete-data" type="button">Delete</button>
  76. </td>
  77. </tr>
  78. <tr data-id="4">
  79. <td class="px-2 py-1">Joshua Malone</td>
  80. <td class="px-2 py-1">(784) 452-6346</td>
  81. <td class="px-2 py-1">[email protected]</td>
  82. <td class="px-2 py-1">P.O. Box 461, 3954 Arcu. Street</td>
  83. <td class="px-2 py-1">Michoac√°n</td>
  84. <td class="px-2 py-1">laoreet ipsum. Curabitur consequat, lectus sit amet luctus vulputate, nisi</td>
  85. <td class="px-2 py-1 text-center align-middle">
  86. <button class="btn btn-danger btn-sm rounded-0 delete-data" type="button">Delete</button>
  87. </td>
  88. </tr>
  89. <tr data-id="5">
  90. <td class="px-2 py-1">Basia Haynes</td>
  91. <td class="px-2 py-1">1-770-885-2056</td>
  92. <td class="px-2 py-1">[email protected]</td>
  93. <td class="px-2 py-1">Ap #187-2876 Phasellus Av.</td>
  94. <td class="px-2 py-1">Henegouwen</td>
  95. <td class="px-2 py-1">sagittis semper. Nam tempor diam dictum sapien. Aenean massa. Integer</td>
  96. <td class="px-2 py-1 text-center align-middle">
  97. <button class="btn btn-danger btn-sm rounded-0 delete-data" type="button">Delete</button>
  98. </td>
  99. </tr>
  100. </tbody>
  101. </table>
  102. </div>
  103. </div>
  104. </div>
  105.  
  106. <!-- Confirmation Modal -->
  107. <div class="modal fade" id="confimartionModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="confimartionModalLabel" aria-hidden="true">
  108. <div class="modal-dialog modal-dialog-centered rounded-0">
  109. <div class="modal-content rounded-0">
  110. <div class="modal-header py-1">
  111. <h5 class="modal-title" id="confimartionModalLabel"></h5>
  112. <button type="button" style="font-size: .65em;" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  113. </div>
  114. <div class="modal-body rounded-0">
  115.  
  116. </div>
  117. <div class="modal-footer py-1 rounded-0">
  118. <button type="button" class="btn btn-primary btn-sm rounded-0 confirm-btn">Continue</button>
  119. <button type="button" class="btn btn-secondary btn-sm rounded-0" data-bs-dismiss="modal">Close</button>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. <!-- End of Confirmation Modal -->
  125. </body>
  126.  
  127. </html>

Creating JavaScript/jQuery Functions

The script below is a javascript file which contains all the jQuery functions for our application. This contains the dynamic modal function, delete function, and the empty table function.

Create a new javascript file and copy/paste the code below and save it as script.js. In my case, all of my JavaScript Files are compiled inside a JS folder which means code below is store to my js folder.

  1. // confirm dialog function
  2. window.confirm_dialog = function($confirmation_title = '', $confirmation_html = '', $callback = '', $callback_param = []) {
  3. // Confirmation Modal
  4. var confirmation_modal = $('#confimartionModal')
  5.  
  6. // Checking if modal exist on the current page
  7. if (confirmation_modal.length <= 0) {
  8. alert("Confimation Modal/Dialog does not exist in this page");
  9. return false;
  10. }
  11.  
  12. // Display Confrimation title
  13. confirmation_modal.find('#confimartionModalLabel').html($confirmation_title);
  14. // Display Confrimation Content
  15. confirmation_modal.find('.modal-body').html($confirmation_html);
  16.  
  17. // Show Confirmation Modal
  18. confirmation_modal.modal('show')
  19.  
  20. // Execute Modal Callback when Continue Button is clicked
  21. confirmation_modal.find('.confirm-btn').click(function() {
  22. if ($callback != '') {
  23. // Checking if callback function exists
  24. if (typeof window[$callback] != 'function') {
  25. alert('Confimartion Dialog\'s Callback Function is undefined.')
  26. } else {
  27.  
  28. // Executing callback
  29. window[$callback]($callback_param);
  30. }
  31. } else {
  32. alert('Confirmation Confirmed but no callback function stated')
  33. }
  34.  
  35. // Close Modal
  36. confirmation_modal.modal('hide')
  37.  
  38. })
  39. return true;
  40. }
  41. window.sample_func = function() {
  42. alert('Confirmed!')
  43. }
  44.  
  45. function delete_row(element) {
  46. action = new Promise(resolve => {
  47. element.hide('slow')
  48. resolve()
  49. })
  50. action.then(() => {
  51. element.remove();
  52. })
  53. }
  54.  
  55. function empty_table() {
  56. $('#dummy-tbl tbody').html('<tr><th colspan="7" class="text-center">No Data Listed</th></tr>')
  57. }
  58.  
  59. $(function() {
  60. $('.delete-data').click(function() {
  61. var tr = $(this).closest('tr')
  62. var name = tr.find('td:nth-child(1)').text()
  63. confirm_dialog('Delete Confirmation', '<p>Are you sure to remove <b>' + name + '</b> from list?</p>', 'delete_row', tr)
  64. })
  65. $('#empty-table').click(function() {
  66. confirm_dialog('Empty Table Confirmation', '<p>Are you sure to the table?</p>', 'empty_table')
  67. })
  68. })

There you go. You can now test the application we created on your end by browsing the index.html file in your browser. To see if the application works properly, each button functionality must open a confirmation dialog when clicked so you as the user can confirm to continue the action or not. Note, the data that has been deleted will be back after reloading the page since the application we created displays static data.

DEMO VIDEO

That's the end of this tutorial. I hope this Creating a Dynamic Confirmation Dialog using jQuery and Bootstrap Modal Tutorial will be helpful for your future web application projects. If you have encountered any errors, review your code or you can also download the working source code I created for this tutorial. The download button is located below this article.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment