Bootstrap modals allow you to pass options through Javascript to do things such as make your modal not closable using backdrop: "static"
.
However, once you initialize a modal without any params $('#myModal').modal();
and then try to call it again this time with different params $('#myModal2').modal({backdrop:"static"});
the modal is still initialized with no params, in this case it is still closable even though I called it again with the backdrop: "static"
option.
Is there a way to reinitialize or change the paramaters of a Bootstrap modal once it has been called?
Here is a snippet showing the issue:
$('body').on('click','#static',function() {
$('#myModal').modal({backdrop:"static"});
});
$('body').on('click','#modal',function() {
$('#myModal').modal();
});
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
<a id="static" href="#myModal" role="button" class="btn" >Static Modal</a>
<a id="modal" href="#myModal" role="button" class="btn">Modal</a>
<div class="modal fade" id="myModal">
Foo
</div>
- Click “Static Modal” and a modal appears that cant be closed.
- Refresh
- Click “Modal” and a modal appears that can be closed, close it
- Click “Static Modal”, now a modal appears that can be closed, when it shouldn’t
2
Answers
I would recommend this Bootstrap Modal plugin to solve your problem quickly:
http://nakupanda.github.io/bootstrap3-dialog/
Back to the question here, by using above plugin, you should be able to achieve by these lines:
This is more like a javascript program, no html markups needed, clean and neat.
One more example in case you would like to change the closing behaviors outside any closures of the dialog (modal) definition:
The 2nd example also demonstrates a handy way to dynamically change modal title and message, and the message can also be set as a closure, which offers you opportunities of building fancy things inside the modal, in JAVASCRIPT fashion.
$.extend( $( '#myModal2' ).data( 'bs.modal' ).options, { backdrop: "static" } );
or
$.extend( $( '#myModal2' ).data( 'modal' ).options, { backdrop: "static" } );