Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent the focus on the toggle button for a Bootstrap modal after the modal closes?

I have a button that toggles a Bootstrap modal. The button itself is wrapped in a div so a tooltip shows up on hover.

When I close the modal, the button gets focused and the tooltip shows without hovering the element.

<span data-toggle="tooltip" data-placement="top" data-title="Tooltip">
    <button  data-toggle="modal" data-target="#modal">Toggle</button>
</span>

 <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-hidden="true">
     <div class="modal-dialog">
         <div class="modal-content">
             <div class="modal-header">
                 <button type="button" class="close" data-dismiss="modal">&times;</button>
             </div>
             
             <div class="modal-body">
                 <p>lorem ispum dolor sit amet</p>
             </div>
             
             
             <div class="modal-footer">
                 <button type="button" class="btn btn-primary">Submit</button>
                 <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
             </div>
         </div>
     </div>
</div>

See here exactly what's happening: http://jsfiddle.net/6t3kxhLb/

The only workaround I could come up with up until now was to blur the button when the hidden.bs.modal event fires up. But I'm not really happy with the result.

This is my workaround:

$('#modal').on('hidden.bs.modal', function(event){
    setTimeout(function(){
      $('[data-toggle="modal"]').blur();
    });
 });

Do you guys know any way to prevent the focus on the toggle button when the modal closes?

like image 402
Mircea Avatar asked Feb 09 '15 13:02

Mircea


People also ask

How do I keep my Bootstrap modal from closing when I click the button?

To prevent closing bootstrap modal when click outside of modal window we need add properties data-backdrop="static" and data-keyboard="false" to button which we are using to show modal window like as shown following.

How do you prevent Bootstrap 5 modal from closing when clicking outside?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the closeOnEscape property value to false to prevent closing of the dialog when pressing Esc key.

How do you stop modal close on outside click?

How do I stop closing the modal when I click outside? Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.

How do I set focus on Bootstrap modal?

Answer: Use the jQuery . focus() method You can simply use the jQuery . focus() method to set the focus on the first input box or textarea inside the Bootstrap modal when it is loaded upon activation.


2 Answers

As per the Bootstrap documentation, you need to specify what triggers the tooltip. The options are click, hover, focus and manual while the default is to have both hover and focus. so just add data-trigger="hover" to your element:

<span data-toggle="tooltip" data-placement="top" data-title="Tooltip" data-trigger="hover">
    <button  data-toggle="modal" data-target="#modal">Toggle</button>
</span>

Example fiddle: http://jsfiddle.net/6t3kxhLb/1/

like image 158
DavidG Avatar answered Sep 26 '22 04:09

DavidG


This is the solution I used, worked like a charm:

$('.modal').on('hidden.bs.modal',function(event){
  event.stopImmediatePropagation();
});
like image 35
Oliver Strobel Avatar answered Sep 26 '22 04:09

Oliver Strobel