Skip to content

Instantly share code, notes, and snippets.

@huytd
Created July 16, 2014 07:51
Show Gist options
  • Save huytd/79165e3e6b186b60f4bd to your computer and use it in GitHub Desktop.
Save huytd/79165e3e6b186b60f4bd to your computer and use it in GitHub Desktop.
Bootstrap Modal Dialog plugin for jQuery
jQuery.showDialog = function (options) {
var settings = $.extend({
confirm: true,
message: "Hurahhh!!!",
success: function () { },
cancel: function () { }
}, options);
var clickedOK = false;
var html = '<div class="modal fade" id="modalDialog" tabindex="-1" role="dialog"> ' +
' <div class="modal-dialog modal-sm"> ' +
' <div class="modal-content"> ' +
' <div class="modal-body"> ' +
settings.message +
' </div> ' +
' <div class="modal-footer"> ';
if (settings.confirm == true) {
html += ' <button type="button" class="btn btn-default" data-dismiss="modal">NO</button> ' +
' <button type="button" id="btnYES" class="btn btn-primary">YES</button> ';
} else {
html += ' <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button> ';
}
html += ' </div> ' +
' </div> ' +
' </div> ' +
'</div> ';
$("body").append(html);
$("#modalDialog").modal().on('hidden.bs.modal', function () {
$("#modalDialog").remove();
if (!clickedOK) {
settings.cancel();
} else {
settings.success();
}
});
$("#modalDialog").on("click", "#btnYES", function () {
clickedOK = true;
$('#modalDialog').modal('hide');
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment