Open In App

jQuery UI | Tooltip

The tooltip widget in jQuery UI is different from the traditional tooltip primarily by making it more themeable and making it much more customizable. Some of the customizations available are:

By default, a fading animation is used to display the tooltip.



Syntax:

$( "#div_tooltip" ).tooltip({
});

Attribute Values:



The tooltip is addable which will override the parent string written with the title attribute. This gives us an advantage as we can change the tooltip by using a script, it is like the same element can show different strings of tooltips under different requirements. Let’s try that. Here we have defined the title as “Welcome to geeksforgeeks” which is overwritten inside the javascript code.
Example 1:




<html>
  
<head>
    <link href=
          rel='stylesheet'>
</head>
  
<body>
    <center>
        <h1 style="color:green">
          GeeksforGeeks
      </h1>
        <div id=div_tooltip 
             title='Welcome to geeksforgeeks'>
            Bring your Mouse here
        </div>
  
        <script src=
        </script>
        <script src=
        </script>
  
        <script>
            $(document).ready(function() {
  
                $("#div_tooltip").tooltip({});
            })
        </script>
    </center>
</body>
  
</html>

Output:

Overwriting the title in Tooltip: Another major advantage of the Tooltip widget in jQuery UI is that the title can be overwritten as specified while defining the element. This can be done by specifying the message inside the content option inside our JavaScript code.

Example 2:




<html>
  
<head>
    <link href=
          rel='stylesheet'>
</head>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <div id=div_tooltip 
             title='Welcome to geeksforgeeks'>
          Bring your Mouse here
      </div>
  
        <script src=
      </script>
        <script src=
      </script>
  
        <script>
            $(document).ready(function() {
                $("#div_tooltip").tooltip({
                    content: "This is my content"
                });
            })
        </script>
    </center>
</body>
  
</html>

Output:

Disabling Tooltip: This attribute when set to true disables the tooltip. By default its value is false.

Example 3:




<html>
  
<head>
    <link href=          
          rel='stylesheet'>
</head>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <div id=div_tooltip
             title='Welcome to geeksforgeeks'>
          Bring your Mouse here
      </div>
  
        <script src=
      </script>
        <script src=
      </script>
  
        <script>
            $(document).ready(function() {
  
                $("#div_tooltip").tooltip({
                    disabled: true
                });
  
            })
        </script>
    </center>
</body>
  
</html>

Output:

Items: The items attribute is used to set a different element message to be displayed as a tooltip. In the below example, one textbox is kept inside the div tag. By default, the message written inside the textbox is to be displayed as a tooltip, but by setting the items to the div tag, and display the message written inside the title attribute of div tag as tooltip.

Example 4:




<html>
  
<head>
    <link href=
          rel='stylesheet'>
</head>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
        <div id=div_tooltip title='I am inside a Div tag'>
            <input type=textbox 
                   name=my_text 
                   id=my_text 
                   title='I am the input box'>
        </div>
  
        <script src=
      </script>
        <script src=
      </script>
  
        <script>
            $(document).ready(function() {
  
                $("#my_text").tooltip({
                    items: 'div'
                });
  
            })
        </script>
    </center>
</body>
  
</html>

Output:

Position: This attribute is used to display the tooltip at any position relative to the main element.
Possible values are.

Also note that:

position: { my: "left center", at: "right top" } 

Here the tooltip’s left side center position (my: “left center”) will be aligned to main elements right side top position (at: “right top”)

Example 5:




<html>
  
<head>
    <link href=
          rel='stylesheet'>
</head>
  
<body>
    <center>
        <h1 style="color:green">GeeksforGeeks</h1>
  
        <div align=center>
            <input type=textbox
                   name=my_text
                   id=my_text
                   title=''>
        </div>
  
        <script src=
        </script>
        <script src=
        </script>
  
        <script>
            $(document).ready(function() {
  
                $("#my_text").tooltip({
                    content: "I am the textbox",
                    position: {
                        my: "left center",
                        at: "right top"
                    }
                });
            })
        </script>
    </center>
</body>
  
</html>

Output:

Show-Hide Tooltip: Show & hide are two independent options but we can learn them together. The show attribute is used to add effects to manage the appearance of the tooltip. This is the animation that is created while showing the tooltip. The hide attribute is used to add effects to manage the disappearance of the tooltip. This is the animation that is created while hiding the tooltip.
For both show and hide options, use several effects in the below example.
Example 6:




<html>
  
<head>
    <link href=
          rel='stylesheet'>
</head>
  
<body>
    <center>
        <h1 style="color:green">Geeksforgeeks</h1>
  
        <input type=textbox 
               name=my_text 
               id=my_text 
               title='I am the input box'>
  
        <script src=
      </script>
        <script src=
      </script>
  
        <script>
            $(document).ready(function() {
  
                $("#my_text").tooltip({
                    show: {
                        effect: "slide",
                        duration: 400
                    },
                    hide: {
                        effect: "pulsate",
                        duration: 400
                    }
                });
            })
        </script>
    </center>
</body>
  
</html>

Output:


Article Tags :