I just had to solve the same problem. The key to getting this to work was that the dialog must be partially initialized in the click event handler for the link you want to use the confirmation functionality with (if you want to use this for more than one link). This is because the target URL for the link must be injected into the event handler for the confirmation button click. I used a CSS class to indicate which links should have the confirmation behavior.

Here's my solution, abstracted away to be suitable for an example.

<div id="dialog" title="Confirmation Required">
  Are you sure about this?
</div>

<script type="text/javascript">
  $(document).ready(function() {
    $("#dialog").dialog({
      autoOpen: false,
      modal: true
    });
  });

  $(".confirmLink").click(function(e) {
    e.preventDefault();
    var targetUrl = $(this).attr("href");

    $("#dialog").dialog({
      buttons : {
        "Confirm" : function() {
          window.location.href = targetUrl;
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog").dialog("open");
  });
</script>

<a class="confirmLink" href="http://someLinkWhichRequiresConfirmation.com">Click here</a>
<a class="confirmLink" href="http://anotherSensitiveLink">Or, you could click here</a>

I believe that this would work for you, if you can generate your links with the CSS class (confirmLink, in my example).

Here is a jsfiddle with the code in it.

In the interest of full disclosure, I'll note that I spent a few minutes on this particular problem and I provided a similar answer to this question, which was also without an accepted answer at the time.

Answer from Paul Morie on Stack Overflow
🌐
Github
craftpip.github.io › jquery-confirm
jquery-confirm.js | The multipurpose alert & confirm
A multipurpose alert & confirm plugin, alternative to the native alert() and confirm() functions. Supports features like auto-close, themes, animations, and more.
🌐
cdnjs
cdnjs.com › home › libraries › jquery-confirm
jquery-confirm - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers
jquery-confirm · A jQuery plugin that provides great set of features like, Auto-close, Ajax-loading, background-dismiss, themes and more. 2k · GitHub · MIT licensed · http://craftpip.github.io/jquery-confirm/ Tags: jquery-plugin, jquery, ...
Discussions

How to implement "confirmation" dialog in Jquery UI dialog? - Stack Overflow
I am try to use JQuery UI Dialog to replace the ugly javascript:alert() box. In my scenario, I have a list of items, and next to each individual of them, I would have a "delete" button for each of ... More on stackoverflow.com
🌐 stackoverflow.com
alerts - Yes or No confirm box using jQuery - Stack Overflow
More shorter return confirm("Are you sure you want to return this meter?") )) 2020-03-25T13:04:19.153Z+00:00 ... Have a look at this jQuery plugin: jquery.confirm. More on stackoverflow.com
🌐 stackoverflow.com
Dialog Confirm
🌐 forum.jquery.com
JQuery Dialog Confirmation
Please see my code below. I have a list of anchors with either a class “confirm” or “button-disabled”. When I click on the anchor via jQuery, the function executes a expected and the dialog opens up but the function does not wait for a response from the dialog. More on sitepoint.com
🌐 sitepoint.com
0
February 26, 2014
🌐
W3Schools
w3schools.com › jsref › met_win_confirm.asp
Window confirm() Method
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate
🌐
npm
npmjs.com › package › jquery-confirm
jquery-confirm - npm
February 7, 2019 - A jQuery plugin that provides great set of features like, Auto-close, Ajax-loading, background-dismiss, themes and more.. Latest version: 3.3.4, last published: 7 years ago. Start using jquery-confirm in your project by running `npm i ...
      » npm install jquery-confirm
    
Published   Feb 07, 2019
Version   3.3.4
Author   Boniface Periera
Top answer
1 of 16
169

I just had to solve the same problem. The key to getting this to work was that the dialog must be partially initialized in the click event handler for the link you want to use the confirmation functionality with (if you want to use this for more than one link). This is because the target URL for the link must be injected into the event handler for the confirmation button click. I used a CSS class to indicate which links should have the confirmation behavior.

Here's my solution, abstracted away to be suitable for an example.

<div id="dialog" title="Confirmation Required">
  Are you sure about this?
</div>

<script type="text/javascript">
  $(document).ready(function() {
    $("#dialog").dialog({
      autoOpen: false,
      modal: true
    });
  });

  $(".confirmLink").click(function(e) {
    e.preventDefault();
    var targetUrl = $(this).attr("href");

    $("#dialog").dialog({
      buttons : {
        "Confirm" : function() {
          window.location.href = targetUrl;
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog").dialog("open");
  });
</script>

<a class="confirmLink" href="http://someLinkWhichRequiresConfirmation.com">Click here</a>
<a class="confirmLink" href="http://anotherSensitiveLink">Or, you could click here</a>

I believe that this would work for you, if you can generate your links with the CSS class (confirmLink, in my example).

Here is a jsfiddle with the code in it.

In the interest of full disclosure, I'll note that I spent a few minutes on this particular problem and I provided a similar answer to this question, which was also without an accepted answer at the time.

2 of 16
60

I found the answer by Paul didn't quite work as the way he was setting the options AFTER the dialog was instantiated on the click event were incorrect. Here is my code which was working. I've not tailored it to match Paul's example but it's only a cat's whisker's difference in terms of some elements are named differently. You should be able to work it out. The correction is in the setter of the dialog option for the buttons on the click event.

$(document).ready(function() {

    $("#dialog").dialog({
        modal: true,
        bgiframe: true,
        width: 500,
        height: 200,
        autoOpen: false
    });


    $(".lb").click(function(e) {

        e.preventDefault();
        var theHREF = $(this).attr("href");

        $("#dialog").dialog('option', 'buttons', {
            "Confirm" : function() {
                window.location.href = theHREF;
            },
            "Cancel" : function() {
                $(this).dialog("close");
            }
        });

        $("#dialog").dialog("open");

    });

});

Hope this helps someone else as this post originally got me down the right track I thought I'd better post the correction.

Find elsewhere
🌐
Medium
diyifang.medium.com › jquery-confirm-a-popup-jquery-plugin-865a7e16a4ce
jquery-confirm: a popup jQuery plugin | Medium
March 11, 2022 - jquery-confirm: a popup jQuery plugin A jQuery plugin that provides a great set of features like Auto-close, Ajax-loading, Themes, Animations, and more. Official site How to use it? Step1: jquery …
🌐
GitHub
github.com › joshmoody › jquery-confirm
GitHub - joshmoody/jquery-confirm: Create native confirmation dialog boxes based on data attributes. · GitHub
Canceling will do nothing, Confirming will allow the default action to occur (like a form submit). <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="//path/to/jquery.confirm.js"></script> <input type="submit" id="confirm-button" class="confirm" data-confirm="Are you sure you want to delete the internet?
Author   joshmoody
🌐
Telerik
telerik.com › javascript › kendo › confirm
confirm - API Reference - Kendo UI kendo - Kendo UI for jQuery
The jQuery Deferred object resolves to: ... <script> kendo.confirm("Confirm text") .done(function(){ /* The result can be observed in the DevTools(F12) console of the browser. */ console.log("User accepted"); }) .fail(function(){ /* The result can be observed in the DevTools(F12) console of ...
🌐
Myclabs
myclabs.github.io › jquery.confirm
jquery.confirm
jquery.confirm Confirm dialogs for buttons and links
🌐
jQuery UI
jqueryui.com › dialog
Dialog | jQuery UI
Modal confirmation · Modal form · Modal message · The basic dialog window is an overlay positioned within the viewport and is protected from page content (like select elements) shining through with an iframe. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default.
🌐
SitePoint
sitepoint.com › javascript
JQuery Dialog Confirmation
February 26, 2014 - Please see my code below. I have a list of anchors with either a class “confirm” or “button-disabled”. When I click on the anchor via jQuery, the function executes a expected and the dialog opens up but the function does not wait for a response from the dialog.
🌐
JSFiddle
jsfiddle.net › nM3Zc › 2771
Yes or No confirm box using jQuery - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
🌐
SitePoint
sitepoint.com › blog › javascript › jquery alert box yes or no
jQuery alert box yes or no — SitePoint
February 29, 2024 - Here’s a simple example: $("#myConfirm").dialog({ autoOpen: false, modal: true, buttons: { "Confirm": function() { // Perform action $(this).dialog("close"); }, "Cancel": function() { $(this).dialog("close"); } } }); Yes, you can create a prompt box using jQuery.
🌐
Caspio
forums.caspio.com › bridge framework › user javascript and css discussions
jquery on 'click' confirm not working
April 6, 2021 - i have a flow of registration forms with an 'exit' url on each page, which allows the users to exit before the full registration is complete. before following the click URL, i want an 'are you sure' confirmation message to be displayed to the ...