44

On my site I have some buttons. When a user clicks the button, a modal opens. When a user hovers the button, a tooltip is shown.

Is use this code:

<button type="button" rel="tooltip" title="Tooltip content" class="btn btn-sm btn-default" data-toggle="modal" data-target="#DeleteUserModal">
  <span class="glyphicon glyphicon-remove"></span>
</button>

<div>modal</div>

<script>
$(document).ready(function(){
  $('[rel="tooltip"]').tooltip();
});
</script>

This works, but the only problem is that the tooltip stays visible after the button is clicked, and the modal is shown. As soon as the modal is closed, the tooltip is hidden again.

How to prevent this? I only want the tooltip to be shown on hover, and not all the time when the related modal is visible.

1
  • use $('[rel=tooltip]').tooltip('disable') in your onclick function Commented Jan 29, 2016 at 8:07

12 Answers 12

64

Fixed it by using.

$(document).ready(function(){
    $('[rel=tooltip]').tooltip({ trigger: "hover" });
});

The problem was that focus stays on the button when the modal is open. Changing the trigger to hover solves the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

This helps, but i still have a similar issue with a dropdown toggle tooltip. On a button with a dropdown, i have a tooltip on the button and when you click it, the dropdown shows but the tooltip remains visibile, tempted to $('body').click( hide all tooltips )
27

For your whole project hide tooltip

   $(document).ready(function(){
       $('[data-toggle="tooltip"]').click(function () {
          $('[data-toggle="tooltip"]').tooltip("hide");

       });
   });

Comments

5

You should open the modal manually with an on click function and hide the tooltip manually via jquery. So take the modal toggling attributes out of the button like so:

<button type="button" rel="tooltip" title="Tooltip content" class="btn btn-sm btn-default">
  <span class="glyphicon glyphicon-remove"></span>
</button>

and then open the modal with your own jquery onclick function as well as hide the tooltip at the same time like so:

$(document).ready(function(){
  $('[rel="tooltip"]').tooltip();
  $('[rel="tooltip').on('click', function () {
    $(this).tooltip('hide');
    $("#DeleteUserModal").modal();
  });
});

Here is a fiddle to show you this working Fiddle

Comments

5

Try to use this Javascript code, I just tried it, and it worked for me.

$(function () {
    $('body').tooltip({
        selector: '[data-toggle="tooltip"]'
    }).click(function () {
        $('[data-toggle="tooltip"]').tooltip("hide");
    });
})

Comments

2

If you want to close the tooltip before an action, can use this:

$('[data-toggle="tooltip"]').tooltip("hide");

For example, i want to close the tooltip before open modal, here my code:

function OpenModal() {
    $('[data-toggle="tooltip"]').tooltip("hide");
    $("#myModal").modal();
}

Comments

2

Inside your document ready function use this:

   $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip({
               trigger : 'hover'
            });
    });

1 Comment

Only thing that worked for me.
1

You could also remove the focus on the button using .blur():

$('[data-toggle="tooltip"]').on('click', function(e){
    $(this).blur();
});

Comments

0

Don't use data attributes from multiple plugins on the same element. For example, a button cannot both have a tooltip and toggle a modal. To accomplish this, use a wrapping element.

3 Comments

I tried wrapping the button with <div rel="tooltip" title="test"></div> and removing the tooltip from the button itself. Same effect.
Add data-toggle="tooltip" don't use rel.
Sorry, same effect. It seems somehow the "focus" stays on the button bacause it's been clicked I can still see the "glow" around the button in the background when the modal is open.
0
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip({ trigger: "hover" });
});

2 Comments

Explanation should help.
While the code given may solve the querent's problem, it would be more helpful to have an explanation of what the code does and why it solves the problem posed. Code without explanation is less useful to future querents looking to solve similar problems.
0

I know this is old but here is the easiest fix for a tooltip on a button to launch a modal ...

<button type="button" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
    <span data-toggle="modal" data-target="#exampleModal">Tooltip & modal</span>
</button>

Put the tooltip and modal on 2 separate elements.

Comments

0

Bootstrap 3 button tooltip hover and click:

$(document).ready(function(){
  $('.btn-primary').tooltip({
        template: '<div class="tooltip svg__icon_c_tooltip_right" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
        title: 'Tooltip Example',
        html: true,
        placement: 'right',
        delay: 250
    });
});

$('.btn-primary').on('shown.bs.tooltip', function() {
    $(this).attr('data-tooltip', 'loaded');
});

$('.btn-primary').on('hide.bs.tooltip', function() {
    $(this).attr('data-tooltip', 'hidden');
});

$('.btn-primary').on('click', function() {
    var _tooltip = $(this).attr('data-tooltip');

    switch(_tooltip) {
    case 'loaded':
      if ($(this).next().hasClass('tooltip')) {
        $(this).tooltip('hide');
      } else {
        $(this).tooltip('show');
      }
      break;
    case 'hidden':
      $(this).tooltip('show');
      break;
    }
});
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <div style="margin-top: 20px">
    <button type="button" class="btn btn-primary">Tooltip</button>
  </div>   
</div>
</body>
</html>

1 Comment

Hi Bondigo. Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. How to Answer. Kind Regards.
0

Bootstrap 5

To programmatically hide a specific tooltip when desired, you must first get a handle to the DOM element that triggers the tooltip, then use the .hide() method.

Example:

const ttEl = document.getElementById('#someElementWithATT');
const tTip = bootstrap.Tooltip.getInstance(ttEl);
tTip.hide();

Reference:

https://getbootstrap.com/docs/5.0/components/tooltips/
(Scroll down to "getInstance" heading)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.