Bootstrap: hover over the tooltip text to click a link
Asked Answered
S

3

5

I am using the tooltip tool in Bootstrap 3.x in my web app to show helpful info. It works in this way:

$('[data-toggle="tooltip"]').tooltip({
        'animation': true,
        'placement': 'top',
        'trigger': 'hover', 
        'html':true,
        'container': 'body'
});

Please note that I have to use hover to trigger the tooltip. Now I need to add a new tooltip and its tooltip text contains a HTML link. A visitor should be able to mouse over the tooltip text to click the link.

The problem is that when a visitor moves his mouse, the tooltip text disappears before he can reach the tooltip text area.

Swimmingly answered 6/6, 2017 at 17:50 Comment(0)
W
8

This is using tooltip now. See if this better answer your question.

var originalLeave = $.fn.tooltip.Constructor.prototype.leave;
$.fn.tooltip.Constructor.prototype.leave = function(obj) {
  var self = obj instanceof this.constructor ?
    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  var container, timeout;

  originalLeave.call(this, obj);

  if (obj.currentTarget) {
    container = $(obj.currentTarget).siblings('.tooltip')
    timeout = self.timeout;
    container.one('mouseenter', function() {
      //We entered the actual popover – call off the dogs
      clearTimeout(timeout);
      //Let's monitor popover content instead
      container.one('mouseleave', function() {
        $.fn.tooltip.Constructor.prototype.leave.call(self, self);
      });
    })
  }
};


$('body').tooltip({
  selector: '[data-toggle]',
  trigger: 'click hover',
  placement: 'auto',
  delay: {
    show: 50,
    hide: 400
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<p>
  <button class='btn btn-primary btn-large' data-toggle="tooltip" data-html="true" title="<a href='http://www.wojt.eu' target='blank' >click me, I'll try not to disappear</a>">hover here</button>
</p>
Woodley answered 6/6, 2017 at 19:12 Comment(2)
It works for my need. It would be ideal that it could work the way you did for popover. Thanks!!!Swimmingly
Nice solution! Quick note that you'll need to instantiate with the delay options for it to work since they are not added by default.Sammons
A
4

This works for me on bootstrap 4.1.3 using documented events (no patching!)

$(document).ready(function() {
    $('body').on('inserted.bs.tooltip', function(e) {
        var $target = $(e.target);

        // Keep track so we can check if mouse is hovering over the tooltip
        $('[role="tooltip"]').hover( function() {
            $(this).toggleClass('hover');
        });

        $target.on('hide.bs.tooltip', function(e) {
            // If tooltip is under the mouse, prevent hide but
            // add handler to hide when mouse leaves tooltip
            if ($('[role="tooltip"]').hasClass('hover')) {
                $('[role="tooltip"]').on('mouseleave', function() {
                    setTimeout(function() {
                            $target.tooltip('hide');
                        }, yourHideDelay);
                });
                // Tell bootstrap tooltip to bail and not actually hide
                e.preventDefault();
                return;
            }
        });
    });
});

It is then possible to select text from the tooltip or click links within it etc.

Allanite answered 8/3, 2019 at 14:43 Comment(1)
I used yours as a reference, but had to do it a bit differently to get it to work pastebin.com/Q21B57UG Thanks!Tractor
W
2

Time gap might solve your problem.

var originalLeave = $.fn.popover.Constructor.prototype.leave;
$.fn.popover.Constructor.prototype.leave = function(obj){
  var self = obj instanceof this.constructor ?
    obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  var container, timeout;

  originalLeave.call(this, obj);

  if(obj.currentTarget) {
    container = $(obj.currentTarget).siblings('.popover')
    timeout = self.timeout;
    container.one('mouseenter', function(){
      //We entered the actual popover – call off the dogs
      clearTimeout(timeout);
      //Let's monitor popover content instead
      container.one('mouseleave', function(){
        $.fn.popover.Constructor.prototype.leave.call(self, self);
      });
    })
  }
};


$('body').popover({ selector: '[data-popover]', trigger: 'click hover', placement: 'auto', delay: {show: 50, hide: 400}});

  
  
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<p id='container'>
<button class='btn btn-primary btn-large' data-popover="true" data-html=true data-content="<a href='http://www.wojt.eu' target='blank' >click me, I'll try not to disappear</a>">hover here</button>
</p>
Woodley answered 6/6, 2017 at 18:13 Comment(2)
Thanks for the solution! I notice that you are not using tooltip. I have been using tooltip everywhere with certain styles in my web app.Swimmingly
I added another answer, it is probably what you need which is tooltipWoodley

© 2022 - 2024 — McMap. All rights reserved.