Calling a function on bootstrap modal open
Bootstrap modal exposes events. Listen for the the shown
event like this
$('#my-modal').on('shown', function(){
// code here
});
you can use show
instead of shown
for making the function to load just before modal open, instead of after modal open.
$('#code').on('show.bs.modal', function (e) {
// do something...
})
will not work.. use $(window)
instead
For Showing
$(window).on('shown.bs.modal', function() {
$('#code').modal('show');
alert('shown');
});
For Hiding
$(window).on('hidden.bs.modal', function() {
$('#code').modal('hide');
alert('hidden');
});
You can use the shown event/show event based on what you need:
$( "#code" ).on('shown', function(){
alert("I want this to appear after the modal has opened!");
});
Demo: Plunker
Update for Bootstrap 3.0
For Bootstrap 3.0 you can still use the shown event but you would use it like this:
$('#code').on('shown.bs.modal', function (e) {
// do something...
})
See the Bootstrap 3.0 docs here under "Events".