How to prevent lost focus in bootstrap modal?

14,827

Solution 1

Along with @SHAZ solution, you also need $(window).focus() function to focus the input when browser tab is in active state

$(function(){
   $('#myModal').on('shown.bs.modal', function () {
      $('#myTextId').focus()
   });
});

$(window).focus(function () {
   var input = document.getElementById("myTextId").focus();
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
      
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <input type="text" id="myTextId" class="form-control" autofocus/>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

Fiddle

Solution 2

Just focus() your input field when bootstrap's modal open everytime. For that use below code:

$(function(){
   $('#myModal').on('shown.bs.modal', function () {
      $('#myTextId').focus()
   });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
      
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <input type="text" id="myTextId" class="form-control" autofocus/>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
Share:
14,827
robsch
Author by

robsch

Peace for Urkaine! Putin's war is the biggest desaster since the last world war. Russion people: Face it! Think about why almost the whole world has a problem with you. That cannot be fake. Protest against Putin's regime, ASAP!!! Software developer in Austria. Loving the idea of StackExchange.

Updated on June 09, 2022

Comments

  • robsch
    robsch almost 2 years

    Have a look on the boostrap site itself:

    http://getbootstrap.com/javascript/#modals-related-target

    Though it has nothing to do with the topic in this paragraph, there you can open a modal with an input field and try the following: Focus an input field, type some chars, switch to another window or browser tab and then come back to the modal. In Firefox the focused input is not focused anymore.

    How could I re-focus the last focused element? Or how could I even prevent the focus loss?

    Update: filed an issue on GitHub.