Calling a function on bootstrap modal open Calling a function on bootstrap modal open javascript javascript

Calling a function on bootstrap modal open


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".


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 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...})