Skip to content

Instantly share code, notes, and snippets.

@danpette
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save danpette/b30bc533c942ed5a2ff2 to your computer and use it in GitHub Desktop.

Select an option

Save danpette/b30bc533c942ed5a2ff2 to your computer and use it in GitHub Desktop.
Bootstrap ModalAlert Function
var modals = 0;
function ModalAlert(title, message, confirm, success_function, size, init_ok_disabled) {
modals++;
var is_confirm_modal = (confirm !== undefined && confirm != null && confirm === true) ? true : false;
var size = (size !== undefined && size != null) ? ' ' + size : '';
var ok_disabled = (init_ok_disabled !== undefined && init_ok_disabled != null) ? init_ok_disabled : false;
console.log(ok_disabled);
jQuery('body').append('<div class="overlay"></div>');
jQuery('body').append('<div class="overlay-modal"><div class="modal"><div class="modal-dialog' + size + '"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">' + title + '</h4></div><div class="modal-body"><p>' + message + '</p></div><div class="modal-footer"><button type="button" class="btn btn-default close-modal-alert" data-dismiss="modal">' + ((is_confirm_modal) ? modal_abort : modal_ok) + '</button>' + ((is_confirm_modal) ? '<button type="button" class="btn btn-primary confirm-modal-alert"' + ((ok_disabled) ? ' disabled="disabled" ' : '') + '>' + modal_accept + '</button>' : '') + '</div></div></div></div></div>');
$('.overlay').css({
"width": "100%",
"height": "100%",
"position": "absolute",
"top": "0",
"left": "0",
"z-index": "1000",
"background-color": "#626262",
"opacity": "0.5",
"filter:": "alpha(opacity=50);"
});
$('.overlay-modal .modal').show();
$('.close-modal-alert').on('click', function () {
modals--;
$(".overlay").eq(modals).hide().remove();
$(".overlay-modal").eq(modals).hide().remove();
return false;
});
$('.modal-header .close').on('click', function () {
modals--;
$(".overlay").eq(modals).hide().remove();
$(".overlay-modal").eq(modals).hide().remove();
return false;
});
$('.confirm-modal-alert').on('click', function () {
modals--;
$(".overlay").eq(modals).hide().remove();
$(".overlay-modal").eq(modals).hide().remove();
success_function();
});
$('.overlay').on('click', function () {
$(this).hide();
$(".overlay-modal").hide();
});
}
Use
ModalAlert("Title","Beskrivelse", true, function(){
//Ajax call or other awesomenes
}, '', false);
@danpette

Copy link
Copy Markdown
Author

Updated with support for multiple stacking overlays

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