253

I used to use jQuery UI's dialog, and it had the open option, where you can specify some Javascript code to execute once the dialog is opened. I would have used that option to select the text within the dialog using a function I have.

Now I want to do that using bootstrap's modal. Below is the HTMl code:

<div id="code" class="modal hide fade"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h3>Modal header</h3> </div> <div class="modal-body"> <pre> print 'Hello World' 

And as for the button that opens the modal:

 <a href="#code" data-toggle="modal" class="btn code-dialog">Display code</a> 

I tried to use an onclick listener of the button, but the alert message was displayed before the modal appeared:

$( ".code-dialog" ).click(function(){ alert("I want this to appear after the modal has opened!"); }); 
3
  • 5
    shown.bs.modal event occurs when HTML document contains at least <div class="modal fade"><div class="modal-dialog"></div></div> structure. Commented Feb 21, 2015 at 3:00
  • dublicate stackoverflow.com/questions/12190119/bootstrap-modal-show-event Commented Feb 27, 2016 at 10:41
  • Greet comment @Chemical Programmer, it should appear with the answer Commented Jan 21, 2020 at 8:49

7 Answers 7

460

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 and 4

For Bootstrap 3.0 and 4.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".

Sign up to request clarification or add additional context in comments.

8 Comments

Use $("#code").on("shown.bs.modal", function(e) {}) for bootstrap 3.0.
The #code refers to the jQuery selector, one of the basic ingredients for jQuery: w3schools.com/jquery/jquery_selectors.asp
I use $(document).on("shown.bs.modal", ... for overall listening
@Xatenev show is at the beginning of the event, shown is at the end. This is consistent across Bootstrap 3 and 4
At least the shown.bs.modal actually executes before the modal is shown. This is not a problem in my case, but may be good to know.
|
121

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'); }); 

6 Comments

$( "#modal" ).on('shown', function(){ alert("I want this to appear after the modal has opened!");} Was not working for me but $(window) worked !! Thanks @viper_tkd
I needed to bind to ANY modal that opened/closed, and not by a specific selector. This answer worked well for me in that case.
This works before modal is fully loaded on GUI. I want to take data from my modal after modal is loaded - this method returns 'undefined' for everything because modal GUI is not yet existing, so fields and everything else is not queryable yet. (not visible)
This worked for me, but I added a check within the handler to target one specific modal: if( $('#code').is( e.relatedTarget ) ) { ... } since I had multiple on the page.
with update to get specific modal in my bootstrap version was: if (shownEvent.target.id == 'uploadTemplateModal'){ }
|
36

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

1 Comment

Perfectly working with me in BS 5 too
11

Bootstrap modal exposes events. Listen for the the shown event like this

$('#my-modal').on('shown', function(){ // code here }); 

1 Comment

Replace shown with shown.bs.modal if using Bootstrap > 3
7

This what worked with me to target specifec modal

$('#code').on('shown.bs.modal', function(){ // code here alert('shown13'); }); 

Comments

1

With bootstrap 5

You can open the modal programmatically and simply call your method afterwards, so:

const modal = new bootstrap.Modal('#myModal') modal.show() alert('modal is showing') 

2 Comments

Here you manually trigger the modal and fire off another manual alert. The original question wanted an event when the modal was shown programmatically. Bootstrap 5 would require binding with the shown event for the modal. myModalEl.addEventListener('shown.bs.modal', event => {}) getbootstrap.com/docs/5.2/components/modal
@WiseGuy Thanks for the response. You're right, but at least for my case the effect was the same, so I'm leaving it, as it might help others as well.
0

if somebody still has a problem the only thing working perfectly for me by useing (loaded.bs.modal) :

 $('#editModal').on('loaded.bs.modal', function () { console.log('edit modal loaded'); $('.datepicker').datepicker({ dateFormat: 'yy-mm-dd', clearBtn: true, rtl: false, todayHighlight: true, toggleActive: true, changeYear: true, changeMonth: true }); }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.