7

What is the best way to make a clickable tooltip like the one in the picture below:

enter image description here

Should I use bootstrap or some other library?

Thanks.

4
  • bootstrap is cool: getbootstrap.com/javascript/#tooltips Commented Oct 7, 2015 at 19:02
  • 1
    bootstrap's popovers are even better: getbootstrap.com/javascript/#popovers Commented Oct 7, 2015 at 19:03
  • Can you expand upon what you expect the widget to do? Commented Oct 7, 2015 at 19:05
  • I want a hover event or click event on the Postsecondary div and then the blue popunder/tooltip to appear. Then the Students or Faculty is also clickable. And lastly if you don't hover over the blue part, it disappears. Commented Oct 7, 2015 at 19:11

3 Answers 3

11

Here You go

$("#Pops").popover({
html: true,
content: function () {
    return $('#popover-content').html();
}
});
[data-style=mypops] + .popover {
background: #4194ca;
}
[data-style=mypops] + .popover.bottom .arrow:after {
border-bottom-color: #4194ca;
}
[data-style=mypops] + .popover-content {
}
.popovermenu {
list-style: none;
padding: 0px;
margin: 0px;
}
.popovermenu li {
}
.popovermenu li a {
color: #fff;
}
<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.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<div class="col-sm-4">
    <button tabindex="0" class="btn btn-default" role="button" data-toggle="popover" data-trigger="focus" data-placement="bottom" data-style="mypops" id="Pops">Click Me</button>
    <div id="popover-content" class="hide">
      <ul class="popovermenu">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Separated link</a></li>
      </ul>
    </div>
</div>

Edit:

  • Added custom data-style="mypops" in popover button and add in css so popover can be customized without effecting the default popover in bootstrap.
  • Replaced data-trigger="click" with data-trigger="focus" in popover button so if click one a link or outside the popover window, popover will be auto closed.

Fiddle

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

3 Comments

how do you get the styles to look like that? my copy just shows the lists
if you used the same code from answer, shouldn't be a problem unless you made any change in it or you are not using bootstrap framework, can you provide a snap shot?
I know this is really an old thread, but I'm curious as how would you do this on Bootstrap 5, but it will trigger the popover on hover?
9

$('[data-toggle="popover"]').popover({ trigger: "manual" , html: true, animation:false})
    .on("mouseenter", function () {
        var _this = this;
        $(this).popover("show");
        $(".popover").on("mouseleave", function () {
            $(_this).popover('hide');
        });
    }).on("mouseleave", function () {
        var _this = this;
        setTimeout(function () {
            if (!$(".popover:hover").length) {
                $(_this).popover("hide");
            }
        }, 300);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!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/4.1.3/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h3>Popover Example</h3>
<a href="#" role="button" class="btn popovers" data-toggle="popover" title="" data-content="test content <a href='https://www.w3schools.com' title='test add link'>link on content</a>" data-original-title="test title">test link</a>
</div>
</body>
</html>

3 Comments

What does the Timer do in MouseLeave?
@geneb Checks that the pointer is within the tooltip or not after 300ms, if not then the tooltip is hidden I reckon
during the mouseenter event you create a new event handler. this could lead to a memory leak.
1

You can use Bootstrap's popover and use the template option to include clickable links in your tooltip. There are also options regarding the tooltip's position.

$(function (){
    $("#example").popover({
        template: '<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>',
        placement: 'right'
    });  
});

http://getbootstrap.com/javascript/#popovers-options

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.