Search code examples
javascriptjquerytwitter-bootstrapbootbox

Convert to Bootbox confirmation


The following code pops up a confirmation windows when the Delete user link is pressed:

<a href="delete_user.php?id=123" onclick="return confirm('Are you sure?');">Delete user</a>

In this case when the OK button is pressed the link delete_user.php?id=123 will be executed. When the Cancel button is pressed nothing will happened.

I would like to do the same thing with Bootbox.

   <a class="alert" href="list_users.php?id=123">Delete user</a>

    <script src="bootbox.min.js"></script>
        <script>
        $(document).on("click", ".alert", function(e) {
            e.preventDefault();

        bootbox.confirm("Are you sure?", function(result) {

            if (result) {
               // What to do here?
            } else {
               // What to do here?
            }               
        });

        });
    </script>

What to do under if(result) and else statements?


Solution

  • This worked for me. Grab the "click" href and use it when you have "result".

      <script>
            $(document).on("click", ".alert", function(e) {
                var link = $(this).attr("href"); // "get" the intended link in a var
                e.preventDefault();    
                bootbox.confirm("Are you sure?", function(result) {    
                    if (result) {
                        document.location.href = link;  // if result, "set" the document location       
                    }    
                });
            });
        </script>