Here we will see different ways to set up Tooltip for our daily use. As we know, tooltip is a really really useful Web feature which helps us to show help text or key information to users interacting with our site.
- Using jQuery
- Using Bootstrap
Using jQuery
Below are the files we need to include in our page to make 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>
- <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 Tooltip on the whole document for all the tags which have attribute title.
We can also apply Tooltip specifically to the tag by using 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 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 tooltip position.
To disable the tooltip we can use 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 setup 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>
Scripts
- <script>
- $(document).ready(function(){
- $('[data-toggle="tooltip"]').tooltip();
- });
- </script>
NoteI will apply Tooltip setup wherever it finds data-toggle attributes in the page.
HTML
- <div class="container">
- <h2>Bootstrap Tooltip Example</h2>
- <br>
- <ul class="list-inline">
- <li><button class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Hello I am on Top">Sample Data - Top</button></li>
- <li><button class="btn btn-info" data-toggle="tooltip" data-placement="bottom" title="Hello I am on Bottom">Sample Data - Bottom</button></li>
- </ul>
- <ul class="list-inline">
- <li><button class="btn btn-primary" data-toggle="tooltip" data-placement="left" title="Hello I am on Left">Sample Data - Left</button></li>
- <li><button class="btn btn-info" data-toggle="tooltip" data-placement="right" title="Hello I am on Right">Sample Data - Right</button></li>
-
- </ul>
- <ul class="list-inline">
-
- <li><button class="btn btn-primary" data-toggle="tooltip" title="Hello I am on Default Top">Sample Data (No Placement)</button></li>
- </ul>
- <br>
- <p>So he we use data-placement attribute setup the tooltip position.</p>
- </div>
Note
So here it first checks the attribute named data-toggle='tooltip' present in any tag and then uses the title attribute to get the text to show as a Tooltip.
For a feature for placement (where you want to show the Tooltip) we need to use another attribute named data-placement and its value could be any one from the below list.
- top (data-placement="top" - so it will show Tooltip on top side of element)
- bottom (data-placement="bottom" - so it will show Tooltip on bottom side of element)
- left (data-placement="left" - so it will show Tooltip on left side of element)
- right (data-placement="right" - so it will show Tooltip on right side of element)
Note
Top is default if you do not supply anything - data-placement attribute is optional
Screens
(Top & Bottom Placement)
(Left & Right Placement)
(Default Placement which is Top)
Other options
We can control specific Tooltip configuration as below.
-
- $('#elementid').tooltip();
We can
customize the look of Tooltip with the help of below classes in css
- <style>
-
- .test + .tooltip > .tooltip-inner {
- background-color: #666;
- color: #FFFFFF;
- border: 1px solid #666;
- padding: 5px;
- }
-
-
-
- .test + .tooltip.top > .tooltip-arrow {
- border-top: 5px solid #666;
- }
-
-
- .test + .tooltip.bottom > .tooltip-arrow {
- border-bottom: 5px solid #666;
- }
-
-
- .test + .tooltip.left > .tooltip-arrow {
- border-left: 5px solid #666;
- }
-
-
- .test + .tooltip.right > .tooltip-arrow {
- border-right: 5px solid #666;
- }
- </style>
We can also show and hide Tooltip on click event as below.
- $(".btn1").click(function(){
- $("[data-toggle='tooltip']").tooltip('show');
- });
- $(".btn2").click(function(){
- $("[data-toggle='tooltip']").tooltip('hide');
- });
Here I have tried to show the basic options which we can use from jQuery and Bootstrap to manage Tooltip easily.
There are other properties available to handle some kind of events with jQuery and Bootstrap Tooltip configuration.
I hope you like this article.
Many thanks for reading!!