Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Twitter Bootstrap, how do I unbind an event from the closing of a modal dialog?

Tags:

I have a function that is bound to the action of hiding a modal dialog.

I'm using code similar to the accepted answer to this question.

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

However, this dialog may get reopened for editing, and, when that happens, I don't necessarily want to run this code. Is there a way to "unbind" the function so that it is no longer run when the dialog closes? I haven't found anything in the documentation.

like image 314
Spike Williams Avatar asked Jan 22 '13 03:01

Spike Williams


People also ask

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.

How do you stop a modal closing?

You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true.

How do you stop modal from closing when clicking outside HTML?

Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.

How do I stop Bootstrap modal pop up?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.


1 Answers

You can do something like to unbind all events tied to the modal element:

Unbind all events in the modal:

/* First option */ $('#myModal').on('hidden', function (e) {     $(e.currentTarget).unbind(); // or $(this)         });  /* Second option is to call it directly when needed */ $('#myModal').unbind(); 

The Bootrap modal also have specific events tied to it, so you can also specify which event(s) you want to unbind.

/* Events are 'hidden', 'hide', 'show', 'shown' */ $('#myModal').unbind(/* specific event here */); 

If you wish to remove events tied to the content of the modal, you can simply just empty the elements within $('#myModal').empty() and unbind those elements appropriately.

like image 180
Dennis Rongo Avatar answered Sep 20 '22 15:09

Dennis Rongo