How to handle the modal closing event in Twitter Bootstrap?
There are two pair of modal events, one is "show" and "shown", the other is "hide" and "hidden". As you can see from the name, hide event fires when modal is about the be close, such as clicking on the cross on the top-right corner or close button or so on. While hidden is fired after the modal is actually close. You can test these events your self. For exampel:
$( '#modal' )
.on('hide', function() {
console.log('hide');
})
.on('hidden', function(){
console.log('hidden');
})
.on('show', function() {
console.log('show');
})
.on('shown', function(){
console.log('shown' )
});
And, as for your question, I think you should listen to the 'hide' event of your modal.
Bootstrap Modal Events:
- hide.bs.modal => Occurs when the modal is about to be hidden.
- hidden.bs.modal => Occurs when the modal is fully hidden (after CSS transitions have completed).
<script type="text/javascript">
$("#salesitems_modal").on('hide.bs.modal', function () {
//actions you want to perform after modal is closed.
});
</script>
I hope this will Help.
If your modal div is dynamically added then use( For bootstrap 3 and 4)
$(document).on('hide.bs.modal','#modal-id', function () {
alert('');
//Do stuff here
});
This will work for non-dynamic content also.
Updated for Bootstrap 3 and 4
Bootstrap 3 and Bootstrap 4 docs refer two events you can use.
hide.bs.modal: This event is fired immediately when the hide instance method has been called.
hidden.bs.modal: This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
And provide an example on how to use them:
$('#myModal').on('hidden.bs.modal', function () {
// do something…
})
Legacy Bootstrap 2.3.2 answer
Bootstrap's documentation refers two events you can use.
hide: This event is fired immediately when the hide instance method has been called.
hidden: This event is fired when the modal has finished being hidden from the user (will wait for css transitions to complete).
And provides an example on how to use them:
$('#myModal').on('hidden', function () {
// do something…
})