Dynamic Bootstrap Modal

Today i will show you how to use single Bootstrap modal in whole application I have created this demo from where you can fetch data from another file or from some id or class in same page

!function(a){a.fn.customBootstrapModal=function(b){function d(){a("#customModal").on("hidden.bs.modal",function()
{a(this).remove()})}var c,d,e={ModalTitle:"Title",dataId:"undefined",CloseText:"Close",modalFooterAlign:"text-
right",size:"modal-lg",button:{label:"Ok",show:!0},button2:{label:"Cancel",show:!0},button3:
{label:"Save",show:!0}},c=a.extend({},e,b);console.log(e);var f=function(){var b,d;b='<div class="modal fade" data-
backdrop="static" id="customModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"><div 
class="modal-dialog '+c.size+'" role="document"><div class="modal-content"><div class="modal-header"><button
 type="button" id="closeModel" class="close" data-dismiss="modal" aria-label="Close"></button><h4 class="modal-title" id="customModalLabel">'+c.ModalTitle+'</h4>
</div><div class="modal-body"></div><div class="modal-footer '+c.modalFooterAlign+'"><button type="button" 
class="btn btn-primary" data-dismiss="modal">'+c.button.label+'</button><button type="button" class="btn btn-
primary" data-dismiss="modal">'+c.button2.label+'</button><button type="button" class="btn btn-primary" data-
dismiss="modal">'+c.button3.label+"</button></div></div></div>
</div>",d=a("body"),d.prepend(b)};this.on("click",function(c){if(f(),a("#customModal").modal("show").find(".modal-
body").load(a(this).attr("href"),function(b,c,d){if("error"==c){a("#customModal").find(".modal-body").html("Sorry 
but there was an error: "+d.status+" "+d.statusText)}}),"undefined"==e.dataId&&a("#customModal").find(".modal-
body").html("Please select the id or class it mush be unique.ERROR"),"undefined"!=b.dataId){var 
d=a("#"+b.dataId).html();a("#customModal").find(".modal-body").html(d)}0==b.button.show&&a("div.modal-foo
ter button:first-child").css("display","none"),0==b.button2.show&&a("div.modal-footer button:first-
child").next().css("display","none"),0==b.button3.show&&a("div.modal-footer button:last-
child").css("display","none"),a("#customModal").modal("show")}),a("body").on("click","#customModal 
button",function(){d()})}}(jQuery);
This is the HTML structure
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>jQuery.extend demo</title>
<link rel=stylesheet type=text/css href=http://getbootstrap.com/dist/css/bootstrap.min.css>
<script src=https://code.jquery.com/jquery-1.10.2.js></script>
<style type=text/css>
.text-center{text-align:center!important}
.text-left{text-align:left!important}
.modal{text-align:center;padding:0!important}
.modal:before{content:'';display:inline-block;height:100%;vertical-align:middle;margin-right:-4px}
.modal-dialog{display:inline-block;text-align:left;vertical-align:middle}
</style>
</head>
<body>
<div id=testdata style=display:none>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
</div>
<button type=button class="btn btn-primary test">Click for modal 1</button>
<button type=button href=remote.html class="btn btn-primary test1">Click for modal 2</button>
<script type=text/javascript src=http://getbootstrap.com/dist/js/bootstrap.min.js></script>
<script type=text/javascript src=modal.js></script>
</body>
</html>

You can Change the modal size,Heading, Close button text,Button maximum 3,Button alignment;

<script>$(".test").customBootstrapModal({
   ModalTitle:"Large Modal Heading",
   dataId:"testdata",
   CloseText:"Close",
   size:"modal-lg",
   modalFooterAlign:"text-left",
   button:{
   label:"Save",show:true},
   button2:{label:"Cancel",show:true},
   button3:{label:"Add",show:true}
});
$(".test1").customBootstrapModal({
   ModalTitle:"Medium Modal Heading",
   CloseText:"X",
   size:"modal-md",
   button:{label:"Save2",show:false},
   button2:{label:"Cancel2",show:false},
   button3:{label:"Add2",show:true}});
</script>

Demo

Leave a Reply

Your email address will not be published. Required fields are marked *