alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow jQuery arrow jQuery Tooltips
Site Search


jQuery Tooltips
Article Index
jQuery Tooltips
Showing, Hiding and Positioning jQuery Tooltips

jQuery Tooltips

Although the row highlighting might be a useful feature, so far it's not apparent to the user that the feature even exists. We can begin to remedy this situation by giving all author cells a clickable class, which will change the cursor to a pointer when a user hovers the mouse cursor over them:

  1. $(document).ready(function() {
  2.  
  3. $('table.striped td:nth-child(' + column + ')' )
  4.  
  5. .addClass('clickable')
  6.  
  7. .click(function() {
  8.  
  9. var thisClicked = $(this).text();
  10.  
  11. $('table.striped td:nth-child(' + column + ')' )
  12.  
  13. .each(function(index) {
  14.  
  15. if (thisClicked == $(this).text()) {
  16.  
  17. $(this).parent().toggleClass('highlight');
  18.  
  19. } else {
  20.  
  21. $(this).parent().removeClass('highlight');
  22.  
  23. };
  24.  
  25. });
  26.  
  27. })
  28.  
  29. })
 

The clickable class is a step in the right direction, for sure, but it still doesn't tell the user what will happen when the cell is clicked. As far as anyone knows (without looking at the code, of course) that clicking could send the user to another page. Some further indication of what will happen upon clicking is in order. Tooltips are a familiar feature of many software applications, including web browsers. We can simulate a tooltip with custom text, such as Click to highlight all rows authored by Rey Bango, when the mouse hovers over one of the author cells. This way, we can alert users to the effect their action will have.

We're going to create three functions—showTooltip, hideTooltip, and positionTooltip—outside any event handlers and then call or reference them as we need them. Let's start with positionTooltip, which we'll reference when the mouse moves over any of the author cells:

  1. var positionTooltip = function(event) {
  2.  
  3. var tPosX = event.pageX - 5;
  4.  
  5. var tPosY = event.pageY + 20;
  6.  
  7. $('div.tooltip').css({top: tPosY, left: tPosX});
  8.  
  9. };
 

Here we use the pageX and pageY properties of event to set the top and left positions of the tooltip. When we reference the function in the .mousemove() method, tPosX will refer to 5 pixels to the left of the mouse cursor while tPosY will refer to 20 pixels below the cursor. We can attach this method to the same chain as the one being used already for .click():

  1. $(document).ready(function() {
  2.  
  3. var positionTooltip = function(event) {
  4.  
  5. var tPosX = event.pageX - 5;
  6.  
  7. var tPosY = event.pageY + 20;
  8.  
  9. $('div.tooltip').css({top: tPosY, left: tPosX});
  10.  
  11. };
  12.  
  13. $('table.striped td:nth-child(' + column + ')' )
  14.  
  15. .addClass('clickable')
  16.  
  17. .click(function() {
  18.  
  19. // ...Code continues...
  20.  
  21. })
  22.  
  23. .mousemove(positionTooltip);
  24.  
  25. });
 

So, we've positioned the tooltip already, but we still haven't created it. That will be done in the showTooltip function.

The first thing that we do in the showTooltip function is remove all tooltips. This may seem counterintuitive, but if we are going to show the tooltip each time the mouse cursor hovers over an author cell; we don't want a proliferation of these tooltips appearing with each new cell hovered over:

  1. var showTooltip = function(event) {
  2.  
  3. $('div.tooltip').remove();
  4.  
  5. };
 

Now we're ready to create the tooltip. We can wrap the entire

and its contents
in a $() function and then append it to the document's body:

 

  1. var showTooltip = function(event) {
  2.  
  3. $('div.tooltip').remove();
  4.  
  5. var $thisAuthor = $(this).text();
  6.  
  7. $('</p>
  8. <div class="tooltip">Click to highlight all articles
  9. written by ' + $thisAuthor + '</div>
  10. ')
  11.  
  12. .appendTo('body');
  13.  
  14. };
 

 

When the mouse cursor hovers over an author cell with Rey Bango in it, the tooltip will read, Click to highlight all articles written by Rey Bango. Unfortunately, the tooltip will appear at the bottom of the page. That's where the positionTooltip function comes in. We simply place that at the end of the showTooltip function:

  1. var showTooltip = function(event) {
  2.  
  3. $('div.tooltip').remove();
  4.  
  5. var $thisAuthor = $(this).text();
  6.  
  7. $('</p>
  8. <div class="tooltip">Click to highlight all articles
  9. written by ' + $thisAuthor + '</div>
  10. ')
  11.  
  12. .appendTo('body');
  13.  
  14. positionTooltip(event);
  15.  
  16. };
 

 

The tooltip still won't be positioned correctly, though, unless we free it from its default postion:static property. We can do that in the stylesheet:

  1. .tooltip {
  2.  
  3. position: absolute;
  4.  
  5. z-index: 2;
  6.  
  7. background: #efd;
  8.  
  9. border: 1px solid #ccc;
  10.  
  11. padding: 3px;
  12.  
  13. }
 

The style rule also gives the tooltip a z-index higher than that of the surrounding elements to ensure that it is layered on top of them, as well as sprucing it up with a background color, a border, and some padding.
Finally, we write a simple hideTooltip function:

  1. var hideTooltip = function() {
  2.  
  3. $('div.tooltip').remove();
  4.  
  5. };
 



 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape