I need to confirm deletion using Bootstrap 3 modal box (YES/NO). How can I create this?
HTML code:
<form action="blah" method="POST">
<button class='btn' type="submit" name="remove_levels" value="delete">
<span class="fa fa-times"></span> Delete
</button>
</form>
This question is related to
jquery
css
twitter-bootstrap-3
bootstrap-modal
confirm
Create modal dialog in HTML with id="confirmation" and use function showConfirmation.
Also remember you should to unbind (modal.unbind()) cancel and success buttons after hide modal dialog. If you do not make this you will get double binding. For example: if you open dialog once and press 'Cancel' and then open dialog in second time and press 'Ok' you will get 2 executions of success callback.
showConfirmation = function(title, message, success, cancel) {
title = title ? title : 'Are you sure?';
var modal = $("#confirmation");
modal.find(".modal-title").html(title).end()
.find(".modal-body").html(message).end()
.modal({ backdrop: 'static', keyboard: false })
.on('hidden.bs.modal', function () {
modal.unbind();
});
if (success) {
modal.one('click', '.modal-footer .btn-primary', success);
}
if (cancel) {
modal.one('click', '.modal-header .close, .modal-footer .btn-default', cancel);
}
};
// bind confirmation dialog on delete buttons
$(document).on("click", ".delete-event, .delete-all-event", function(event){
event.preventDefault();
var self = $(this);
var url = $(this).data('url');
var success = function(){
alert('window.location.href=url');
}
var cancel = function(){
alert('Cancel');
};
if (self.data('confirmation')) {
var title = self.data('confirmation-title') ? self.data('confirmation-title') : undefined;
var message = self.data('confirmation');
showConfirmation(title, message, success, cancel);
} else {
success();
}
});
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
You can use Bootbox
dialog boxes
$(document).ready(function() {
$('#btnDelete').click(function() {
bootbox.confirm("Are you sure want to delete?", function(result) {
alert("Confirm result: " + result);
});
});
});
simple way to use modals is with eModal!
Ex from github:
<script src="//rawgit.com/saribe/eModal/master/dist/eModal.min.js"></script>
var options = {_x000D_
message: "The famouse question?",_x000D_
title: 'Header title',_x000D_
size: 'sm',_x000D_
callback: function(result) { result ? doActionTrue(result) : doActionFalse(); },_x000D_
subtitle: 'smaller text header',_x000D_
label: "True" // use the possitive lable as key_x000D_
//..._x000D_
};_x000D_
_x000D_
eModal.confirm(options);
_x000D_
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>_x000D_
<script src="//rawgit.com/saribe/eModal/master/dist/eModal.min.js"></script>
_x000D_
Tip: You can use change the default label name! { label: 'Yes' | 'True'| 'OK' }
I've the same problem just today. This is my solution (which I think is better and simpler):
<!-- Modal dialog -->
<div class="modal fade" id="frmPrenotazione" tabindex="-1">
<!-- CUTTED -->
<div id="step1" class="modal-footer">
<button type="button" class="glyphicon glyphicon-erase btn btn-default" id="btnDelete"> Delete</button>
</div>
</div>
<!-- Modal confirm -->
<div class="modal" id="confirmModal" style="display: none; z-index: 1050;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body" id="confirmMessage">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" id="confirmOk">Ok</button>
<button type="button" class="btn btn-default" id="confirmCancel">Cancel</button>
</div>
</div>
</div>
</div>
And in my .js:
$('#btnDelete').on('click', function(e){
confirmDialog(YOUR_MESSAGE_STRING_CONST, function(){
//My code to delete
});
});
function confirmDialog(message, onConfirm){
var fClose = function(){
modal.modal("hide");
};
var modal = $("#confirmModal");
modal.modal("show");
$("#confirmMessage").empty().append(message);
$("#confirmOk").unbind().one('click', onConfirm).one('click', fClose);
$("#confirmCancel").unbind().one("click", fClose);
}
Using unbind
before the one
prevents that the removal function is invoked at the next opening of the dialog.
I hope this could be helpful.
Follow a complete example:
var YOUR_MESSAGE_STRING_CONST = "Your confirm message?";_x000D_
$('#btnDelete').on('click', function(e){_x000D_
confirmDialog(YOUR_MESSAGE_STRING_CONST, function(){_x000D_
//My code to delete_x000D_
console.log("deleted!");_x000D_
});_x000D_
});_x000D_
_x000D_
function confirmDialog(message, onConfirm){_x000D_
var fClose = function(){_x000D_
modal.modal("hide");_x000D_
};_x000D_
var modal = $("#confirmModal");_x000D_
modal.modal("show");_x000D_
$("#confirmMessage").empty().append(message);_x000D_
$("#confirmOk").unbind().one('click', onConfirm).one('click', fClose);_x000D_
$("#confirmCancel").unbind().one("click", fClose);_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>_x000D_
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
_x000D_
<!-- Modal dialog -->_x000D_
<div id="frmTest" tabindex="-1">_x000D_
<!-- CUTTED -->_x000D_
<div id="step1" class="modal-footer">_x000D_
<button type="button" class="glyphicon glyphicon-erase btn btn-default" id="btnDelete"> Delete</button>_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
<!-- Modal confirm -->_x000D_
<div class="modal" id="confirmModal" style="display: none; z-index: 1050;">_x000D_
<div class="modal-dialog">_x000D_
<div class="modal-content">_x000D_
<div class="modal-body" id="confirmMessage">_x000D_
</div>_x000D_
<div class="modal-footer">_x000D_
<button type="button" class="btn btn-default" id="confirmOk">Ok</button>_x000D_
<button type="button" class="btn btn-default" id="confirmCancel">Cancel</button>_x000D_
</div>_x000D_
</div>_x000D_
</div>_x000D_
</div>
_x000D_
Following solution is better than bootbox.js, because
digimango.messagebox.js:
const dialogTemplate = '\_x000D_
<div class ="modal" id="digimango_messageBox" role="dialog">\_x000D_
<div class ="modal-dialog">\_x000D_
<div class ="modal-content">\_x000D_
<div class ="modal-body">\_x000D_
<p class ="text-success" id="digimango_messageBoxMessage">Some text in the modal.</p>\_x000D_
<p><textarea id="digimango_messageBoxTextArea" cols="70" rows="5"></textarea></p>\_x000D_
</div>\_x000D_
<div class ="modal-footer">\_x000D_
<button type="button" class ="btn btn-primary" id="digimango_messageBoxOkButton">OK</button>\_x000D_
<button type="button" class ="btn btn-default" data-dismiss="modal" id="digimango_messageBoxCancelButton">Cancel</button>\_x000D_
</div>\_x000D_
</div>\_x000D_
</div>\_x000D_
</div>';_x000D_
_x000D_
_x000D_
// See the comment inside function digimango_onOkClick(event) {_x000D_
var digimango_numOfDialogsOpened = 0;_x000D_
_x000D_
_x000D_
function messageBox(msg, significance, options, actionConfirmedCallback) {_x000D_
if ($('#digimango_MessageBoxContainer').length == 0) {_x000D_
var iDiv = document.createElement('div');_x000D_
iDiv.id = 'digimango_MessageBoxContainer';_x000D_
document.getElementsByTagName('body')[0].appendChild(iDiv);_x000D_
$("#digimango_MessageBoxContainer").html(dialogTemplate);_x000D_
}_x000D_
_x000D_
var okButtonName, cancelButtonName, showTextBox, textBoxDefaultText;_x000D_
_x000D_
if (options == null) {_x000D_
okButtonName = 'OK';_x000D_
cancelButtonName = null;_x000D_
showTextBox = null;_x000D_
textBoxDefaultText = null;_x000D_
} else {_x000D_
okButtonName = options.okButtonName;_x000D_
cancelButtonName = options.cancelButtonName;_x000D_
showTextBox = options.showTextBox;_x000D_
textBoxDefaultText = options.textBoxDefaultText;_x000D_
}_x000D_
_x000D_
if (showTextBox == true) {_x000D_
if (textBoxDefaultText == null)_x000D_
$('#digimango_messageBoxTextArea').val('');_x000D_
else_x000D_
$('#digimango_messageBoxTextArea').val(textBoxDefaultText);_x000D_
_x000D_
$('#digimango_messageBoxTextArea').show();_x000D_
}_x000D_
else_x000D_
$('#digimango_messageBoxTextArea').hide();_x000D_
_x000D_
if (okButtonName != null)_x000D_
$('#digimango_messageBoxOkButton').html(okButtonName);_x000D_
else_x000D_
$('#digimango_messageBoxOkButton').html('OK');_x000D_
_x000D_
if (cancelButtonName == null)_x000D_
$('#digimango_messageBoxCancelButton').hide();_x000D_
else {_x000D_
$('#digimango_messageBoxCancelButton').show();_x000D_
$('#digimango_messageBoxCancelButton').html(cancelButtonName);_x000D_
}_x000D_
_x000D_
$('#digimango_messageBoxOkButton').unbind('click');_x000D_
$('#digimango_messageBoxOkButton').on('click', { callback: actionConfirmedCallback }, digimango_onOkClick);_x000D_
_x000D_
$('#digimango_messageBoxCancelButton').unbind('click');_x000D_
$('#digimango_messageBoxCancelButton').on('click', digimango_onCancelClick);_x000D_
_x000D_
var content = $("#digimango_messageBoxMessage");_x000D_
_x000D_
if (significance == 'error')_x000D_
content.attr('class', 'text-danger');_x000D_
else if (significance == 'warning')_x000D_
content.attr('class', 'text-warning');_x000D_
else_x000D_
content.attr('class', 'text-success');_x000D_
_x000D_
content.html(msg);_x000D_
_x000D_
if (digimango_numOfDialogsOpened == 0)_x000D_
$("#digimango_messageBox").modal();_x000D_
_x000D_
digimango_numOfDialogsOpened++;_x000D_
}_x000D_
_x000D_
function digimango_onOkClick(event) {_x000D_
// JavaScript's nature is unblocking. So the function call in the following line will not block,_x000D_
// thus the last line of this function, which is to hide the dialog, is executed before user_x000D_
// clicks the "OK" button on the second dialog shown in the callback. Therefore we need to count_x000D_
// how many dialogs is currently showing. If we know there is still a dialog being shown, we do_x000D_
// not execute the last line in this function._x000D_
if (typeof (event.data.callback) != 'undefined')_x000D_
event.data.callback($('#digimango_messageBoxTextArea').val());_x000D_
_x000D_
digimango_numOfDialogsOpened--;_x000D_
_x000D_
if (digimango_numOfDialogsOpened == 0)_x000D_
$('#digimango_messageBox').modal('hide');_x000D_
}_x000D_
_x000D_
function digimango_onCancelClick() {_x000D_
digimango_numOfDialogsOpened--;_x000D_
_x000D_
if (digimango_numOfDialogsOpened == 0)_x000D_
$('#digimango_messageBox').modal('hide');_x000D_
}
_x000D_
To use digimango.messagebox.js:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">_x000D_
<html xmlns="http://www.w3.org/1999/xhtml">_x000D_
<head>_x000D_
<title>A useful generic message box</title>_x000D_
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />_x000D_
_x000D_
<link rel="stylesheet" type="text/css" href="~/Content/bootstrap.min.css" media="screen" />_x000D_
<script src="~/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>_x000D_
<script src="~/Scripts/bootstrap.js" type="text/javascript"></script>_x000D_
<script src="~/Scripts/bootbox.js" type="text/javascript"></script>_x000D_
_x000D_
<script src="~/Scripts/digimango.messagebox.js" type="text/javascript"></script>_x000D_
_x000D_
_x000D_
<script type="text/javascript">_x000D_
function testAlert() {_x000D_
messageBox('Something went wrong!', 'error');_x000D_
}_x000D_
_x000D_
function testAlertWithCallback() {_x000D_
messageBox('Something went wrong!', 'error', null, function () {_x000D_
messageBox('OK clicked.');_x000D_
});_x000D_
}_x000D_
_x000D_
function testConfirm() {_x000D_
messageBox('Do you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' }, function () {_x000D_
messageBox('Are you sure you want to proceed?', 'warning', { okButtonName: 'Yes', cancelButtonName: 'No' });_x000D_
});_x000D_
}_x000D_
_x000D_
function testPrompt() {_x000D_
messageBox('How do you feel now?', 'normal', { showTextBox: true }, function (userInput) {_x000D_
messageBox('User entered "' + userInput + '".');_x000D_
});_x000D_
}_x000D_
_x000D_
function testPromptWithDefault() {_x000D_
messageBox('How do you feel now?', 'normal', { showTextBox: true, textBoxDefaultText: 'I am good!' }, function (userInput) {_x000D_
messageBox('User entered "' + userInput + '".');_x000D_
});_x000D_
}_x000D_
_x000D_
</script>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
<a href="#" onclick="testAlert();">Test alert</a> <br/>_x000D_
<a href="#" onclick="testAlertWithCallback();">Test alert with callback</a> <br />_x000D_
<a href="#" onclick="testConfirm();">Test confirm</a> <br/>_x000D_
<a href="#" onclick="testPrompt();">Test prompt</a><br />_x000D_
<a href="#" onclick="testPromptWithDefault();">Test prompt with default text</a> <br />_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
$('.launchConfirm').on('click', function (e) {
$('#confirm')
.modal({ backdrop: 'static', keyboard: false })
.one('click', '#delete', function (e) {
//delete function
});
});
For your button:
<button class='btn btn-danger btn-xs launchConfirm' type="button" name="remove_levels"><span class="fa fa-times"></span> delete</button></td>
Source: Stackoverflow.com