Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you enable the escape key close functionality in a Twitter Bootstrap modal?

People also ask

How can I close a modal popup on hitting escape?

This modal can be closed with the escape key on your keyboard. Note: You need to press the tab key on the keyboard to first enter the modal window, and then press the Esc key.

How do you make a button close on a modal?

The close button can be added by using the simple markup with CSS class. The main container for close button is the <button> tag with .

How do I keep my bootstrap modal from closing when I click outside?

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.

What is data-keyboard?

The data-keyboard attribute specifies whether the modal can be closed with the escape key. Note: Press the Tab key on your keyboard to enter the modal window and then press the Esc key.


It looks like this is an issue with how the keyup event is being bound.

You can add the tabindex attribute to you modal to get around this issue:

tabindex="-1"

So your full code should look like this:

<a href="#my-modal" data-keyboard="true" data-toggle="modal">Open Modal</a>

<div class='modal fade hide' id='my-modal' tabindex='-1'>
    <div class='modal-body'>
    <div>Test</div>
    </div>
</div>

For more info you can view the discussion on this issue on github

(Updated link to new TWBS repository)


also if you're invoking via javascript , use this:

$('#myModal').modal({keyboard: true}) 

add tabindex="-1" attribute to modal div

<div id="myModal" class="modal fade" role="dialog" tabindex="-1">

</div>

In angular I am using like this:

var modalInstance = $modal.open({                        
 keyboard: false,
 backdrop: 'static',
 templateUrl: 'app/dashboard/view/currentlyIneligibleView.html',
 controller: 'currentlyIneligibleCtrl',
 resolve: {
     data: function () { return param; }
    }
});
  • backdrop: 'static' => Stop to close on clicking outside
  • keyboard: false => Stop to close by using escape buttton