1

I do not want Bootstrap Dropdowns to close when the ESC-Key is being pressed.

I tried the following snippet without any success:

$(document).on('shown.bs.dropdown', function (e) {
    $(document).on("keydown", $button, function (e) {
        var code = e.keyCode || e.which;
        if (code === 27) {
            e.preventDefault();
        }
    });
});

I found a similar Question, which is about disabling the Key for Bootstraps Modals. The solution for that seems to be data-keyboard="false". Is there a similar solution for Dropdowns?

Edit: See JSFiddle

Community
  • 1
  • 1
LocalHorst
  • 1,048
  • 2
  • 11
  • 24

1 Answers1

0

I use following:

$('#dropdown_id').on('hide.bs.dropdown', function (e) {
  if ($(this).hasClass('keepopen')) {
      $(this).removeClass('keepopen')
      e.preventDefault();
      e.stopPropagation();
      return false;
  }
});

Add class 'keepopen' when you expect dropdown to close and want to prevent it. In my case it is typing in the input element inside the dropdown:

$('#input_id').on('keydown', function(e){
  if (e.which == 27) {
    $('#dropdown_id').addClass('keepopen');
  }
});