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


jQuery Tooltips
has the highlight class, we add un- in front of the word highlight when we create the tooltip:

  1. $(document).ready(function() {
  2.  
  3. var highlighted = "";
  4.  
  5. // Code continues...
  6.  
  7. var showTooltip = function(event) {
  8.  
  9. $('div.tooltip').remove();
  10.  
  11. var $thisAuthor = $(this).text();
  12.  
  13. if ($(this).parent().is('.highlight')) {
  14.  
  15. highlighted = 'un-';
  16.  
  17. } else {
  18.  
  19. highlighted = '';
  20.  
  21. };
  22.  
  23. $('</p>
  24. <div class="tooltip">Click to '
  25.  
  26. + highlighted + 'highlight all articles written by '
  27.  
  28. + $thisAuthor + '</div>
  29. ').appendTo('body');
  30.  
  31. positionTooltip(event);
  32.  
  33. };
  34.  
  35. };
 

 

Our tooltip task would now be finished were it not for the need to trigger the tooltip-changing behavior when a cell is clicked as well. For that, we need to call the showTooltip function inside the .click() event handler:

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

By using the JavaScript call() function, we can invoke showTooltip as if it were defined within the .click() handler. Therefore, this inherits the scope of .click(). Additionally, we pass in event so that we can use its pageX and pageY information for the positioning.

Now the tooltip offers a more intelligent suggestion when the hovered row is already highlighted.


Trackback(0)
Comments (0)add comment

Write comment

busy

Article Index
jQuery Tooltips
Showing, Hiding and Positioning jQuery Tooltips

Now that we have functions for showing, hiding, and positioning the tooltip, we can reference them at the appropriate places in our code:

  1. $(document).ready(function() {
  2.  
  3. var column = 3;
  4.  
  5. // Position the tooltip.
  6.  
  7. var positionTooltip = function(event) {
  8.  
  9. var tPosX = event.pageX - 5;
  10.  
  11. var tPosY = event.pageY + 20;
  12.  
  13. $('div.tooltip').css({top: tPosY, left: tPosX});
  14.  
  15. };
  16.  
  17. // Show (create) the tooltip.
  18.  
  19. var showTooltip = function(event) {
  20.  
  21. $('div.tooltip').remove();
  22.  
  23. var $thisAuthor = $(this).text();
  24.  
  25. $('</p>
  26. <div class="tooltip">Click to highlight all articles written
  27. by ' + $thisAuthor + '</div>
  28. ').appendTo('body');
  29.  
  30. positionTooltip(event);
  31.  
  32. };
  33.  
  34. // Hide (remove) the tooltip.
  35.  
  36. var hideTooltip = function() {
  37.  
  38. $('div.tooltip').remove();
  39.  
  40. };
  41.  
  42. $('table.striped td:nth-child(' + column + ')' )
  43.  
  44. .addClass('clickable')
  45.  
  46. .click(function(event) {
  47.  
  48. var thisClicked = $(this).text();
  49.  
  50. $('table.striped td:nth-child(' + column + ')'
  51.  
  52. ).each(function(index) {
  53.  
  54. if (thisClicked == $(this).text()) {
  55.  
  56. $(this).parent().toggleClass('highlight');
  57.  
  58. } else {
  59.  
  60. $(this).parent().removeClass('highlight');
  61.  
  62. };
  63.  
  64. });
  65.  
  66. })
  67.  
  68. .hover(showTooltip, hideTooltip)
  69.  
  70. .mousemove(positionTooltip);
  71.  
  72. })
 

 

Note that the .hover() and .mousemove() methods are referencing functions that are defined elsewhere. As such, the functions take no parentheses. Also, because positionTooltip(event) is called inside showTooltip, the tooltip is immediately positioned on hover; it then continues to be referenced as the mouse cursor is moved over the cell due to the function's placement inside the .mousemove() method. The tooltip now looks like this:

Everything works fine now, with a tooltip that appears when we hover over an author cell, moves with the mouse movement, and disappears when we move the mouse cursor out of the cell. The only problem is that the tooltip continues to suggest clicking on a cell to highlight the articles even after those articles have been highlighted:

What we need is a way to change the tooltip if the row has the highlight class. Fortunately, we have the showTooltip function, in which we can place a conditional test to check for the class. If the current cell's parent

 
< 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