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?
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?
Share Improve this question edited Jun 18, 2013 at 16:04 madth3 7,34412 gold badges52 silver badges74 bronze badges asked Jun 7, 2013 at 15:00 OualidOualid 4031 gold badge9 silver badges23 bronze badges 5 |2 Answers
Reset to default 19This 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>
This 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();
}
});
});
if (result) document.location.href = this.attr('href');
. Noelse
required. – Julian H. Lam Commented Jun 7, 2013 at 15:06this.href
? – Julian H. Lam Commented Jun 11, 2013 at 16:12