Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Tooltips

 

Showing, Hiding and Positioning jQuery Tooltips

Page 2 of 2


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



Sample Code
  1. $(document).ready(function() {
  2. var column = 3
  3. // Position the tooltip.
  4. var positionTooltip = function(event) {
  5. var tPosX = event.pageX - 5
  6. var tPosY = event.pageY + 20
  7. $('div.tooltip').css({top: tPosY, left: tPosX})
  8. }
  9. // Show (create) the tooltip.
  10. var showTooltip = function(event) {
  11. $('div.tooltip').remove()
  12. var $thisAuthor = $(this).text()
  13. $('</p><br>
  14. <div class="tooltip">Click to highlight all articles written
  15. by ' + $thisAuthor + '</div>
  16. ').appendTo('body')
  17. positionTooltip(event)
  18. }
  19. // Hide (remove) the tooltip.
  20. var hideTooltip = function() {
  21. $('div.tooltip').remove()
  22. }
  23. $('table.striped td:nth-child(' + column + ')' )
  24. .addClass('clickable')
  25. .click(function(event) {
  26. var thisClicked = $(this).text()
  27. $('table.striped td:nth-child(' + column + ')'
  28. ).each(function(index) {
  29. if (thisClicked == $(this).text()) {
  30. $(this).parent().toggleClass('highlight')
  31. } else {
  32. $(this).parent().removeClass('highlight')
  33. }
  34. })
  35. })
  36. .hover(showTooltip, hideTooltip)
  37. .mousemove(positionTooltip)
  38. })
Copyright exforsys.com


 


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 has the highlight class, we add un- in front of the word highlight when we create the tooltip:


Sample Code
  1. $(document).ready(function() {
  2. var highlighted = ""
  3. // Code continues...
  4. var showTooltip = function(event) {
  5. $('div.tooltip').remove()
  6. var $thisAuthor = $(this).text()
  7. if ($(this).parent().is('.highlight')) {
  8. highlighted = 'un-'
  9. } else {
  10. highlighted = ''
  11. }
  12. $('</p><br>
  13. <div class="tooltip">Click to '
  14. + highlighted + 'highlight all articles written by '
  15. + $thisAuthor + '</div>
  16. ').appendTo('body')
  17. positionTooltip(event)
  18. }
  19. }
Copyright exforsys.com


 


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:


Sample Code
  1. $(document).ready(function() {
  2. // Code continues...
  3. .click(function(event) {
  4. var thisClicked = $(this).text()
  5. $('table.striped td:nth-child(' + column + ')'
  6. ).each(function(index) {
  7. if (thisClicked == $(this).text()) {
  8. $(this).parent().toggleClass('highlight')
  9. } else {
  10. $(this).parent().removeClass('highlight')
  11. }
  12. })
  13. showTooltip.call(this, event)
  14. })
  15. // Code continues...
  16. })
Copyright exforsys.com



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.





First Page: jQuery Tooltips


Read Next: jQuery Collapsing and Expanding Table Rows



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape