Skip to content

Instantly share code, notes, and snippets.

@pennstatephil
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pennstatephil/9896793 to your computer and use it in GitHub Desktop.
Save pennstatephil/9896793 to your computer and use it in GitHub Desktop.
Use "confirm" attribute on Link/Button to use bootbox confirm window
function initConfirm() {
$("[confirm]:not(.confirmInitialized)").each(function () {
$(this).click(function (e) {
e.preventDefault();
var action = function () { alert("no action"); };
switch ($(this).prop('tagName')) {
case 'A':
var url = $(this).attr("href"),
replaceId = $(this).attr("ajax-replaceTargetId"),
ajaxCompleteFunc = $(this).attr("ajax-complete");
if (!replaceId) {
action = function () { document.location.href = url; };
}
else {
action = function () {
$.ajax({
url: url
})
.done(function (html) {
$('#' + replaceId).html(html);
//reinit other js here if needed
if (ajaxCompleteFunc) {
var onComplete = new Function (ajaxCompleteFunc);
onComplete();
}
});
}
}
break;
case 'BUTTON':
var form = $(this).closest('form');
if ($(this).attr("name") != "" && $(this).attr("value") != "") {
var foundIndex = $(form).attr('action').indexOf($(this).attr("name"));
if (foundIndex == -1) //don't add the same param over and over
{
$(form).attr('action', $(form).attr('action') + "?" + $(this).attr("name") + "=" + $(this).attr("value"));
}
else //else replace it
{
$(form).attr('action', $(form).attr('action').substring(0, foundIndex) + $(this).attr("name") + "=" + $(this).attr("value"));
}
}
action = function () { form.submit(); };
break;
}
bootbox.confirm($(this).attr("confirm"), function (result) {
if (result)
action();
});
});
$(this).addClass("confirmInitialized");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment