Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 980
Oualid Avatar asked Jun 07 '13 15:06

Oualid


1 Answers

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>
like image 141
Sylvain Jacob Avatar answered Oct 09 '22 23:10

Sylvain Jacob