Convert to Bootbox Confirmation

The following code displays confirmation windows when you click the Delete user link:

<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 delete_user.php link will be executed? id = 123. When the Cancel button is clicked, nothing will happen.

I would like to do the same 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 in if (result) and else operations?

+7
source share
2 answers

It worked for me. Take the "click" href and use it when you have a "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> 
+19
source

It works great!

 $(".alert").on("click", function (e) { // Init var self = $(this); e.preventDefault(); // Show Message bootbox.confirm("Are you sure?", function (result) { if (result) { self.off("click"); self.click(); } }); }); 
+2
source

All Articles