Iman Sedighi Iman Sedighi - 4 months ago 82
jQuery Question

Bind a function to Bootstrap Modal Close by ESC key

I know we can bind a function to bootstrap modal for close event like this:

$("#myModal").on('', function(){
//run the script

But this only works when you close the modal by clicking on grey area or clicking on close button in firefox. However whenever I try to close the modal by ESC key my script is running. I don't want to disable to ESC key for closing bootstrap modal.
Is there any solution to detect closing modal by pressing escape in bootstrap in firefox?

You can check a sample
jsfiddle here. In this example In firefox if you play the YouTube video in the modal with and close the modal by pressing escape the video doesn't stop but if you click on close button or click on grey area then the close functions works properly and clear the iframe src so video stops.


Put below code for close event of your modal. It will also work with esc button.

$("#myModal").on('', function(){

Here is the fiddle for this.