Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a function to Twitter Bootstrap Modal Close

I am using the Twitter Bootstrap lib on a new project and I want for part of the page to refresh and retrieve the latest json data on modal close. I dont see this anywhere in the documentation can someone point it out to me or suggest a solution.

Two problems with using the documented methods

 $('#my-modal').bind('hide', function () {    // do something ...  }); 

I attach a "hide" class to the modal already so it does not display on page load so that would load twice

even if I remove the hide class and set the element id to display:none and add console.log("THE MODAL CLOSED"); to the function above when I hit close nothing happens.

like image 360
BillPull Avatar asked Dec 02 '11 23:12

BillPull


People also ask

How do you call a function on modal close?

if you want to fire a function on every modal close, you can use this method, $(document). ready(function (){ $('. modal').

How do I manually close a Bootstrap modal?

Answer: Use the modal('hide') Method You can simply use the modal('hide') method to hide or close the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('show') and modal('toggle') .

How do I stop a Bootstrap modal from closing?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.


2 Answers

Bootstrap 3 & 4

$('#myModal').on('hidden.bs.modal', function () {     // do something… }); 

Bootstrap 3: getbootstrap.com/javascript/#modals-events

Bootstrap 4: getbootstrap.com/docs/4.1/components/modal/#events

Bootstrap 2.3.2

$('#myModal').on('hidden', function () {     // do something… }); 

See getbootstrap.com/2.3.2/javascript.html#modals → Events

like image 115
Ricardo Lima Avatar answered Oct 15 '22 10:10

Ricardo Lima


Bootstrap 4

$('#my-modal').on('hidden.bs.modal', function () {   window.alert('hidden event fired!'); }); 

See this JSFiddle for a working example:

https://jsfiddle.net/6n7bg2c9/

See the Modal Events section of the docs here:

https://getbootstrap.com/docs/4.3/components/modal/#events

like image 29
aar0n Avatar answered Oct 15 '22 10:10

aar0n