Skip to content

Instantly share code, notes, and snippets.

@lankymart
Last active September 24, 2019 11:00
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 lankymart/38695d7990b922130287 to your computer and use it in GitHub Desktop.
Save lankymart/38695d7990b922130287 to your computer and use it in GitHub Desktop.
FancyBox2 Confirm Dialog Box
function fancyconfirm(msg, options, callback) {
$.fancybox("#confirm", {
modal: options.modal,
beforeShow: function () {
this.content.prepend("<p class=\"title\"></p>");
$(".title").html(msg);
if (options.buttons == null) {
options.buttons = [{
clsName: "yes",
title: "Yes",
value: true
}, {
clsName: "no",
title: "No",
value: false
}];
}
for (i = 0; i < options.buttons.length; i++) {
this.content.append($("<input>", {
type: "button",
class: "confirm " + options.buttons[i].class,
value: options.buttons[i].title
}).data("index", i).css("margin-left", ((i > 0) ? "10px" : "")));
}
$(this.content).css("text-align", "center");
},
afterShow: function () {
$(".confirm").on("click", function (event) {
ret = options.buttons[$(event.target).data("index")].value;
$.fancybox.close();
});
},
afterClose: function () {
this.content.html("");
callback.call(this, ret);
}
});
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>FancyBox2 - Confirm Box Demo</title>
<link rel="stylesheet" type="text/css" href="http://fancyapps.com/fancybox/source/jquery.fancybox.css">
<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
</head>
<body>
<a class="fancybox" href="#">click here</a>
<div id="confirm"></div>
<script type='text/javascript' src="http://fancyapps.com/fancybox/source/jquery.fancybox.js"></script>
<script type="text/javascript" src="fancyconfirm.js"></script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {
$(".fancybox").on("click", function (e) {
e.preventDefault();
fancyconfirm("Do you wish to continue?", {
/*
Define your buttons here, can handle multiple buttons
with custom title and values
*/
buttons: [{
class: "yes",
type: "button",
title: "Yes",
value: "yes"
}, {
class: "no",
type: "button",
title: "No",
value: "no"
}, {
class: "maybe",
type: "button",
title: "Maybe?",
value: "maybe"
}],
modal: true
},
function (resp) {
alert("You clicked " + resp);
});
});
});
//]]>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment