Last active
August 29, 2015 13:57
-
-
Save pennstatephil/9896793 to your computer and use it in GitHub Desktop.
Use "confirm" attribute on Link/Button to use bootbox confirm window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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