Lets use an example to show how the Tooltips can be added to any HTML element in your document.
NOTE:
If these tooltip samples don't work when you put them in your page, then you have another problem. You need to look at things like:
- The order of scripts being included
- See if you are trying to initialize HTML elements that have been removed
- See if you are trying to call methods in JS files you are no longer including
See if you are including the JS file that provides the functionality you need (not just for tooltips, but any components you use on the page).
<head>
<link rel="stylesheet" href="/Content/bootstrap.css" />
<link rel="stylesheet" href="/Content/animate.css" />
<link rel="stylesheet" href="/Content/style.css" />
</head>
<body>
... your HTML code ...
<script src="/Scripts/jquery-2.1.1.js"></script>
<script src="/Scripts/bootstrap.min.js"></script>
<script src="/Scripts/app/inspinia.js"></script> <!-- if using INSPINIA -->
... your JavaScript initializers ...
</body>
Failure of ANY of the above items can (and often does) prevent javascript from loading and/or running, and that keeps everything nice and broken.
WORKING EXAMPLES
Let's say you have a badge, and you want it to show a tooltip when the user hovers over it.
Original HTML:
<span class="badge badge-sm badge-plain">Admin Mode</span>
Plain Bootstrap Tooltips
If you are creating tooltips for an HTML Element, and you are using Plain Bootstrap tooltips, then you will be responsible for calling the Tooltip Initializers with your own JavaScript code.
BEFORE
<span class="badge badge-sm badge-plain">Admin Mode</span>
AFTER
<span
class="badge badge-sm badge-plain"
data-toggle="tooltip"
data-placement="right"
title="Tooltip on right"
>Admin Mode</span>
INITIALIZER
<script>
// Initialize any Tooltip on this page
$(document).ready(function ()
{
$('[data-toggle="tooltip"]').tooltip();
}
);
</script>
Bootstrap Template Tooltips (such as INSPINIA)
If you are using a bootstrap template (such as INSPINIA), you are including a supporting script to support the template features:
<script src="/Scripts/app/inspinia.js" />
In the case of INSPINIA, the included script automatically initializes all tooltips by running the following javascript code when the document is finished loading:
// Tooltips demo
$('.tooltip-demo').tooltip({
selector: "[data-toggle=tooltip]",
container: "body"
});
Because of this, you do NOT have to initialize INSPINIA-style Tooltips yourself. But you DO have to format your elements in a specific way. The Initializer looks for HTML elements with tooltip-demo
in the class
attribute, then calls the tooltip()
method to initialize any child elements that have the attribute data-toggle="tooltip"
defined.
For our example badge, place an outer element around it (like a <div>
or <span>
) that has class="tooltip-demo"
, then place the data-toggle
, data-placement
, and title
attributes for the actual tooltip within the element that is the badge. Modify the original HTML from above to look something like this:
BEFORE
<span class="badge badge-sm badge-plain">Admin Mode</span>
AFTER
<span class="tooltip-demo">
<span
class="badge badge-sm badge-plain"
data-toggle="tooltip"
data-placement="right"
title="Tooltip on right"
>Admin Mode</span>
</span>
INITIALIZER
None
Note that any child elements within the <span class="tooltip-demo">
element will have their tooltip properly prepared. I could have three child elements, all needing tooltips, and place them within one container.
Multiple Items, each with a Tooltip
<span class="tooltip-demo">
<span class="badge badge-sm badge-plain" data-toggle="tooltip" data-placement="right" title="A Tooltip">Text 001</span>
<span class="badge badge-sm badge-plain" data-toggle="tooltip" data-placement="right" title="Another Tooltip">Text 002</span>
<span class="badge badge-sm badge-plain" data-toggle="tooltip" data-placement="right" title="Third Tooltip">Text 003</span>
</span>
The best use for this would be to add the class="tooltip-demo"
to a <td>
or an outermost <div>
or <span>
.
Plain Bootstrap Tooltips while using a Template
If you are using INSPINIA, but you don't want to add extra outer <div>
or <span>
tags to create tooltips, you can use standard Bootstrap Tooltips without interfering with the template. In this case, you will be responsible for initializing the Tooltips yourself. However, you should use a custom value in the class
attribute to identify your Tooltip items. This will keep your Tooltip initializer from interfering with elements affected by INSPINIA. In our example, let's use standalone-tt
:
BEFORE
<span class="badge badge-sm badge-plain">Admin Mode</span>
AFTER
<span
class="standalone-tt badge badge-sm badge-plain"
data-toggle="tooltip"
data-placement="right"
title="Tooltip on right"
>Admin Mode</span>
INITIALIZER
<script>
// Initialize MY standalone tooltips, ignoring INSPINIA-affected elements
$(document).ready(function ()
{
$('.standalone-tt').tooltip();
}
);
</script>
– Mike Bartlett Feb 25 '12 at 20:33