• We just launched and are currently in beta. Join us as we build and grow the community.

Custom Confirmation Modal in JavaScript Tutorial

MKS_269

Console Collector
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
153
Likes
43
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <head

    >
  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. <body

    >
  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. function

    action2(

    )

    {
  8. var

    result =

    "Action 2 has been clicked"

    ;
  9. var

    action_display =

    $(

    '#action_display'

    )
  10. action_display.find

    (

    '.modal-body'

    )

    .html

    (

    result)
  11. action_display.modal

    (

    'show'

    )
  12. }

  13. function

    action3(

    $param1 =

    ''

    )

    {
  14. var

    result =

    "Action 3 has been clicked. Parameter = "

    +

    $param1;
  15. var

    action_display =

    $(

    '#action_display'

    )
  16. action_display.find

    (

    '.modal-body'

    )

    .html

    (

    result)
  17. action_display.modal

    (

    'show'

    )

  18. }

  19. function

    action4(

    $param1 =

    ''

    ,

    $param2 =

    ''

    )

    {
  20. var

    result =

    "Action 1 has been clicked. parameter 1 = \'

    "

    +

    $param1 +

    "\'

    , parameter 2 = \'

    "

    +

    $param2 +

    "\'

    "

    ;
  21. var

    action_display =

    $(

    '#action_display'

    )
  22. action_display.find

    (

    '.modal-body'

    )

    .html

    (

    result)
  23. action_display.modal

    (

    'show'

    )

  24. }
  25. window._confirm =

    function

    (

    $message =

    ''

    ,

    $func =

    ''

    ,

    $param =

    [

    ]

    )

    {
  26. if

    (

    $func !=

    ''

    &&

    typeof

    window[

    $func]

    ==

    'function'

    )

    {

  27. var

    modal_el =

    $(

    '#confirm_modal'

    )
  28. modal_el.find

    (

    '.modal-body'

    )

    .html

    (

    $message)
  29. modal_el.modal

    (

    'show'

    )
  30. modal_el.find

    (

    '#confirm-btn'

    )

    .click

    (

    function

    (

    e)

    {
  31. e.preventDefault

    (

    )
  32. if

    (

    $param.length

    >

    0

    &&

    !!

    $.isArray

    (

    $param)

    )
  33. window[

    $func]

    .apply

    (

    this

    ,

    $param)
  34. else
  35. window[

    $func]

    (

    $param)
  36. modal_el.modal

    (

    'hide'

    )
  37. }

    )
  38. }

    else

    {
  39. alert(

    "Function does not exists."

    )
  40. }
  41. }
  42. $(

    function

    (

    )

    {
  43. $(

    "#action1"

    )

    .click

    (

    function

    (

    )

    {
  44. _confirm(

    "Are you sure to continue to execute action 1?"

    ,

    'action1'

    )
  45. }

    )
  46. $(

    "#action2"

    )

    .click

    (

    function

    (

    )

    {
  47. _confirm(

    "Are you sure to continue to execute action 2?"

    ,

    'action2'

    )
  48. }

    )
  49. $(

    "#action3"

    )

    .click

    (

    function

    (

    )

    {
  50. _confirm(

    "Are you sure to continue to execute action 3?"

    ,

    'action3'

    ,

    'Single Parameter'

    )
  51. }

    )
  52. $(

    "#action4"

    )

    .click

    (

    function

    (

    )

    {
  53. _confirm(

    "Are you sure to continue to execute action 4?"

    ,

    'action4'

    ,

    [

    'Hello World!'

    ,

    "SourceCodester"

    ]

    )
  54. }

    )
  55. }

    )

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 :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top