Skip to content

Instantly share code, notes, and snippets.

@jnormore
Last active November 10, 2019 13:07
Show Gist options
  • Save jnormore/7418776 to your computer and use it in GitHub Desktop.
Save jnormore/7418776 to your computer and use it in GitHub Desktop.
Bootstrap 3 alert and confirm modals. Overrides window.alert normally, but window.confirm override requires a callback function to be passed to get result. Optional arguments for modal title and confirm button label.
window.alert = function(message, title) {
if($("#bootstrap-alert-box-modal").length == 0) {
$("body").append('<div id="bootstrap-alert-box-modal" class="modal fade">\
<div class="modal-dialog">\
<div class="modal-content">\
<div class="modal-header" style="min-height:40px;">\
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\
<h4 class="modal-title"></h4>\
</div>\
<div class="modal-body"><p></p></div>\
<div class="modal-footer">\
<a href="#" data-dismiss="modal" class="btn btn-default">Close</a>\
</div>\
</div>\
</div>\
</div>');
}
$("#bootstrap-alert-box-modal .modal-header h4").text(title || "");
$("#bootstrap-alert-box-modal .modal-body p").text(message || "");
$("#bootstrap-alert-box-modal").modal('show');
};
window.confirm = function(message, title, yes_label, callback) {
$("#bootstrap-confirm-box-modal").data('confirm-yes', false);
if($("#bootstrap-confirm-box-modal").length == 0) {
$("body").append('<div id="bootstrap-confirm-box-modal" class="modal fade">\
<div class="modal-dialog">\
<div class="modal-content">\
<div class="modal-header" style="min-height:40px;">\
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\
<h4 class="modal-title"></h4>\
</div>\
<div class="modal-body"><p></p></div>\
<div class="modal-footer">\
<a href="#" data-dismiss="modal" class="btn btn-default">Cancel</a>\
<a href="#" class="btn btn-primary">' + (yes_label || 'OK') + '</a>\
</div>\
</div>\
</div>\
</div>');
$("#bootstrap-confirm-box-modal .modal-footer .btn-primary").on('click', function () {
$("#bootstrap-confirm-box-modal").data('confirm-yes', true);
$("#bootstrap-confirm-box-modal").modal('hide');
return false;
});
$("#bootstrap-confirm-box-modal").on('hide.bs.modal', function () {
if(callback) callback($("#bootstrap-confirm-box-modal").data('confirm-yes'));
});
}
$("#bootstrap-confirm-box-modal .modal-header h4").text(title || "");
$("#bootstrap-confirm-box-modal .modal-body p").text(message || "");
$("#bootstrap-confirm-box-modal").modal('show');
};
Copy link

ghost commented Mar 21, 2018

works fine on BS2 too. thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment