
Pop it all over
In some cases, you may want to show more information that does not fit in a simple tooltip component. For that, Bootstrap has created popovers, which are components that create small overlays of content to show detailed secondary information.
The popover plugin is an extension of the tooltip plugin, so if you are using separate plugins, you must load both to make it work. Also, just like tooltips, popovers cannot be activated simply through data attributes. You must call them via JavaScript to make them work.
Let's use a popover in our web app example, on the right-hand-side column, the one identified by div#who-follow
. We will add the popover to the Follow buttons, and for that, we need to do two things. The first one is to change the <button>
element to an <a>
element and then add the popover markup.
First, about the <buttons>
inside the div#who-follow
element. Change them to <a>
elements in the HTML. Also add the role="button"
and tabindex="-1"
attributes to the links to fix the issue of cross-browser compatibility:
<div id="who-follow" class="card"> <div class="card-header"> Who to follow </div> <div class="card-block"> <ul class="list-unstyled"> <li> <img src="imgs/cat.jpg" class="img-rounded"> <div class="info"> <strong>Crazy cats</strong> <a href="#" role="button" tabindex="-1" class="btn btn-default"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Follow </a> </div> </li> <li> <img src="imgs/ration.jpg" class="img-rounded"> <div class="info"> <strong>Free ration alert</strong> <a href="#" role="button" tabindex="-1" class="btn btn-default"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Follow </a> </div> </li> </ul> </div>
The code in bold refers to the changes from button to link. Now, we must add the popover markup. It is pretty simple and follows most of the data attributes presented in the tooltip plugin:
<div id="who-follow" class="card"> <div class="card-header"> Who to follow </div> <div class="card-block"> <ul class="list-unstyled"> <li> <img src="imgs/cat.jpg" class="img-rounded"> <div class="info"> <strong>Crazy cats</strong> <a href="#" role="button" tabindex="-1" class="btn btn-default" data-toggle="popover" data-trigger="focus" title="You may want to follow"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Follow </a> </div> </li> <li> <img src="imgs/ration.jpg" class="img-rounded"> <div class="info"> <strong>Free ration alert</strong> <a href="#" role="button" tabindex="-1" class="btn btn-default" data-toggle="popover" data-trigger="focus" title="You may want to follow"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Follow </a> </div> </li> </ul> </div>
Just like the popover, add the following line to the JavaScript code to make popovers appear:
$(document).ready(function() { … // the rest of the JavaScript $('[data-toggle="popover"]').popover(); });
Refresh the web browser, click on the Follow button and see the popover appearing to the right of the button. Now we will make some changes using the options to customize it. First of all, let's create the content that will appear inside the popover and change its placement in JavaScript:
$(document).ready(function() { … // rest of the JavaScript var popoverContentTemplate = '' + '<img src="imgs/breed.jpg" class="img-rounded">' + '<div class="info">' + '<strong>Dog Breeds</strong>' + '<a href="#" class="btn btn-default">' + '<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>' + 'Follow' + '</a>' + '</div>'; $('[data-toggle="popover"]').popover({ placement: 'bottom', html: true, content: function() { return popoverContentTemplate; } }); });
In the preceding code, we changed the placement of the popover to bottom
and set the content that will appear inside the popover to be HTML with the html: true
option. The content was provided by a function that simply returned the popoverContentTemplate
variable.
For instance, we could have used the template in very different ways that are more optimized, but we did this to show the method of adding HTML content onto a popover via JavaScript and using a function for that. We could have called and used some options of the target clicked button inside the function by accessing the current scope in the this
variable.
Popover events
Popovers and tooltips provide some nice events. As was said before, Bootstrap triggers some events when plugin elements appear, hide, and are inserted. To play with these, let's use the show.bs.popover
event, which is an event that is fired immediately when the popover
is show
. In this case, we want to create an action before the popover
is show
. We want to change the text of the Follow button that we clicked on to Following, while changing the icon next to the text from a plus sign to an okay sign. We can take advantage of the show.bs.popover
Bootstrap event to make these changes. In the JavaScript file, insert the following delegation to the popovers:
$(document).ready(function() { … // the rest of the JavaScript code $('[data-toggle="popover"]').on('show.bs.popover', function() { var $icon = $(this).find('span.glyphicon'); $icon.removeClass('glyphicon-plus').addClass('glyphicon-ok'); $(this).append('ing'); }); });
The scope of this event is the element of data-toggle
, which is the Follow button. We query the icon inside the button, and change it from glyphicon-plus
to glyphicon-ok
. Finally, we append the infinitive ing
to Follow, which means that we are now following Crazy cats or Free ration alert suggestions:
To add a cherry to the pie, let's change the color of the icon from blue to green when the okay icon appears:
div#who-follow li .info .glyphicon-ok { color: #5cb85c; }
Refresh the web browser and click on the Follow button. You should see something similar to this screenshot:

There are many other places where the Bootstrap events can be used. This is a nice example where we want to change the element that we are interacting with. Keep in mind to change it whenever you need some related interaction.