Introduction
Here, we will see different ways to set up a Tooltip for our daily use. As we know, a tooltip is a really useful Web feature that helps us to show help text or key information to users interacting with our site.
The simple definition of Tooltip is that it could be text, an image, or something else that appears when you move your mouse cursor over html elements, images, hyperlinks/ anchors, or something else that makes up the graphical user interface (GUI).
I am going to show you different ways to show a Tooltip.
Using jQuery
Using Bootstrap
Using jQuery
Below are the files we need to include in our page to make the jQuery Tooltip work.
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
HTML
<p>
<a href="#" id="ancTag" title="Hi I am tooltip on Anchor">Tool Tip on Anchor Tag</a>
</p>
<p>
<label>Your Age:</label>
<input id="txtAge" title="The age should be above 18">
</p>
<div title="I am a tooltip of Div" style="border: solid 1px #ddd; background-color: #eee; display: inline-block; padding: 5px;">
Hey I am Div
</div>
Script
<script type="text/javascript">
$(document).ready(function() {
$(document).tooltip();
});
</script>
Here, it will apply a Tooltip to the whole document for all the tags that have the attribute title.
We can also apply Tooltip specifically to the tag by using an element ID like below, not the whole document.
<script type="text/javascript">
$(document).ready(function() {
$("#txtAge").tooltip();
$("#ancTag").tooltip();
$("#div").tooltip();
});
</script>
Screen

Note. Here, it directly uses the title attribute from our HTML tag to get the text to show as a Tooltip
Other options we will learn with the jQuery Tooltip.
content
track
disabled
position
If we want to set the tooltip text dynamically, we can use the content option like below, with other options above.
<script>
$(function() {
$("#txtAge").tooltip({
content: "<strong>I am from script</strong>",
track: true
});
$("#div").tooltip({
disabled: true
});
});
</script>
Here we are setting tooltip text from script with track: true, which means it will follow the mouse to show the tooltip position.
To disable the tooltip, we can use the disabled: true property.
To set the tooltip position, we can use position attributes like below.
<script>
$(function() {
$("#ancTag").tooltip({
position: {
my: "center bottom",
at: "center top-10",
collision: "none"
}
});
});
</script>
Tooltip will appear based on the position properties set up like above.
Using Bootstrap
Below are the files we need to include in our page to make a Bootstrap Tooltip work.
<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>




Pankajkumar PatelPosted Sep 4, 2020, 12:08 AM
Given good examples.