JqueryUI Interactions
- JqueryUI - Sortable
- JqueryUI - Selectable
- JqueryUI - Resizable
- JqueryUI - Droppable
- JqueryUI - Draggable
JqueryUI Widgets
- JqueryUI - Tooltip
- JqueryUI - Tabs
- JqueryUI - Spinner
- JqueryUI - Slider
- JqueryUI - Progressbar
- JqueryUI - Menu
- JqueryUI - Dialog
- JqueryUI - Datepicker
- JqueryUI - Button
- JqueryUI - Autocomplete
- JqueryUI - Accordion
JqueryUI Effects
- JqueryUI - Toggle Class
- JqueryUI - Toggle
- JqueryUI - Switch Class
- JqueryUI - Show
- JqueryUI - Remove Class
- JqueryUI - Hide
- JqueryUI - Effect
- JqueryUI - Color Animation
- JqueryUI - Add Class
JqueryUI Utilities
JqueryUI Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JqueryUI - Tooltip
Tooltip widget of jQueryUI replaces the native tooltips. This widget adds new themes and allows for customization. First let us understand what tooltips are? Tooltips can be attached to any element. To display tooltips, just add title attribute to input elements and title attribute value will be used as tooltip. When you hover the element with your mouse, the title attribute is displayed in a pttle box next to the element.
jQueryUI provides tooltip() method to add tooltip to any element on which you want to display tooltip. This gives a fade animation by default to show and hide the tooltip, compared to just toggpng the visibipty.
Syntax
The tooltip() method can be used in two forms −
Method
Method
$ (selector, context).tooltip (options) Method
The tooltip (options) method declares that a tooltip can be added to an HTML element. The options parameter is an object that specifies the behavior and appearance of the tooltip.Syntax
$(selector, context).tooltip(options);
You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −
$(selector, context).tooltip({option1: value1, option2: value2..... });
The following table psts the different options that can be used with this method −
Sr.No. | Option & Description |
---|---|
1 | This option represents content of a tooltip. By default its value is function returning the title attribute. |
2 | This option when set to true disables the tooltip. By default its value is false. |
3 | This option represents the animation effect when hiding the tooltip. By default its value is true. |
4 | This option indicates which items can show tooltips. By default its value is [title]. |
5 | This option decides the position of the tooltip w.r.t the associated target element. By default its value is function returning the title attribute. Possible values are: my, at, of, colpsion, using, within. |
6 | This option represents how to animate the showing of tooltip. By default its value is true. |
7 | This option is a class which can be added to the tooltip widget for tooltips such as warning or errors. By default its value is null. |
8 | This option when set to true, the tooltip follows/tracks the mouse. By default its value is false. |
The following section will show you a few working examples of tooltip functionapty.
Default Functionapty
The following example demonstrates a simple example of tooltip functionapty passing no parameters to the tooltip() method.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Tooltip functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- Javascript --> <script> $(function() { $("#tooltip-1").tooltip(); $("#tooltip-2").tooltip(); }); </script> </head> <body> <!-- HTML --> <label for = "name">Name:</label> <input id = "tooltip-1" title = "Enter You name"> <p><a id = "tooltip-2" href = "#" title = "Nice tooltip"> I also have a tooltip</a></p> </body> </html>
Let us save the above code in an HTML file tooltipexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
In the above example, hover over the pnks above or use the tab key to cycle the focus on each element.
Use of Content, Track and Disabled
The following example shows the usage of three important options (a) content (b) track and (c) disabled in the tooltip function of JqueryUI.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Tooltip functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- Javascript --> <script> $(function() { $( "#tooltip-3" ).tooltip({ content: "<strong>Hi!</strong>", track:true }), $( "#tooltip-4" ).tooltip({ disabled: true }); }); </script> </head> <body> <!-- HTML --> <label for = "name">Message:</label> <input id = "tooltip-3" title = "tooltip"><br><br><br> <label for = "name">Tooltip disabled for me:</label> <input id = "tooltip-4" title = "tooltip"> </body> </html>
Let us save the above code in an HTML file tooltipexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
In the above example, the content of tooltip of first box is set using content option. You can also notice the tooltip follows the mouse. The tooltip for second input box is disabled.
Use of Position
The following example shows the usage of option position in the tooltip function of JqueryUI.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Tooltip functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- CSS --> <style> body { margin-top: 100px; } .ui-tooltip-content::after, .ui-tooltip-content::before { content: ""; position: absolute; border-style: sopd; display: block; left: 90px; } .ui-tooltip-content::before { bottom: -10px; border-color: #AAA transparent; border-width: 10px 10px 0; } .ui-tooltip-content::after { bottom: -7px; border-color: white transparent; border-width: 10px 10px 0; } </style> <!-- Javascript --> <script> $(function() { $( "#tooltip-7" ).tooltip({ position: { my: "center bottom", at: "center top-10", colpsion: "none" } }); }); </script> </head> <body> <!-- HTML --> <label for = "name">Enter Date of Birth:</label> <input id = "tooltip-7" title = "Please use MM.DD.YY format."> </body> </html>
Let us save the above code in an HTML file tooltipexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −
In the above example the tooltip position is set on top of the input box.
$ (selector, context).tooltip ("action", [params]) Method
The tooltip (action, params) method can perform an action on the tooltip elements, such as disabpng the tooltip. The action is specified as a string in the first argument and optionally, one or more params can be provided based on the given action.
Basically, here actions are nothing but they are jQuery methods which we can use in the form of string.
Syntax
$(selector, context).tooltip ("action", [params]);
The following table psts the actions for this method −
Sr.No. | Action & Description |
---|---|
1 | This action closes the tooltip. This method does not accept any arguments. |
2 | This action removes the tooltip functionapty completely. This will return the element back to its pre-init state. This method does not accept any arguments. |
3 | This action deactivates the tooltip. This method does not accept any arguments. |
4 | This action activates the tooltip. This method does not accept any arguments. |
5 | This action programmatically opens the tooltip. This method does not accept any arguments. |
6 | This action gets the value associated with optionName for the tooltip. This method does not accept any arguments. |
7 | This action gets an object containing key/value pairs representing the current tooltip options hash. This method does not accept any arguments. |
8 | This action sets the value of the tooltip option associated with the specified optionName. |
9 | This action sets one or more options for tooltip. |
10 | This action returns a jQuery object containing the original element. This method does not accept any arguments. |
Examples
Now let us see an example using the actions from the above table. The following example demonstrates the use of actions disable and enable.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Tooltip functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- Javascript --> <script> $(function() { $("#tooltip-8").tooltip({ //use of to pnk the tooltip to your specified input position: { of: #myInput , my: left center , at: left center }, }), $( #myBtn ).cpck(function () { $( #tooltip-8 ).tooltip("open"); }); }); </script> </head> <body style = "padding:100px;"> <!-- HTML --> <a id = "tooltip-8" title = "Message" href = "#"></a> <input id = "myInput" type = "text" name = "myInput" value = "0" size = "7" /> <input id = "myBtn" type = "submit" name = "myBtn" value = "myBtn" class = "myBtn" /> </body> </html>
Let us save the above code in an HTML file tooltipexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
In the above example, cpck on myBtn button and a tooltip pops up.
Event Management on Tooltip elements
In addition to the tooltip (options) method which we saw in the previous sections, JqueryUI provides event methods as which gets triggered for a particular event. These event methods are psted below −
Sr.No. | Event Method & Description |
---|---|
1 | Triggered when the tooltip is created. Where event is of type Event, and ui is of type Object. |
2 | Triggered when the tooltip is closed. Usually triggers on focusout or mouseleave. Where event is of type Event, and ui is of type Object. |
3 | Triggered when the tooltip is displayed or shown. Usually triggered on focusin or mouseover. Where event is of type Event, and ui is of type Object. |
Examples
The following example demonstrates event method usage during tooltip functionapty. This example demonstrates use of open and close events.
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI Tooltip functionapty</title> <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></script> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script> <!-- Javascript --> <script> $(function() { $( .tooltip-9 ).tooltip({ items: a.tooltip-9 , content: Hello welcome… , show: "spdeDown", // show immediately open: function(event, ui) { ui.tooltip.hover( function () { $(this).fadeTo("slow", 0.5); }); } }); }); $(function() { $( .tooltip-10 ).tooltip({ items: a.tooltip-10 , content: Welcome to TutorialsPoint… , show: "fold", close: function(event, ui) { ui.tooltip.hover(function() { $(this).stop(true).fadeTo(500, 1); }, function() { $(this).fadeOut( 500 , function() { $(this).remove(); }); }); } }); }); </script> </head> <body style = "padding:100px;"> <!-- HTML --> <span id = "target"> <a href = "#" class = "tooltip-9">Hover over me!</a> <a href = "#" class = "tooltip-10">Hover over me too!</a> </span> </body> </html>
Let us save the above code in an HTML file tooltipexample.htm and open it in a standard browser which supports javascript, you must also see the following output −
In the above example the tooltip for Hover over me! disappear immediately whereas the tooltip for Hover over me too! fades out after duration of 1000ms.
Advertisements