Exforsys.com
 

Sponsored Links

 

jQuery Tutorials

 
Home Tutorials jQuery
 

jQuery Tooltips

 

jQuery Tooltips

Page 1 of 2

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:



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



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:


Sample Code
  1. var positionTooltip = function(event) {
  2. var tPosX = event.pageX - 5
  3. var tPosY = event.pageY + 20
  4. $('div.tooltip').css({top: tPosY, left: tPosX})
  5. }
Copyright exforsys.com



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():


Sample Code
  1. $(document).ready(function() {
  2. var positionTooltip = function(event) {
  3. var tPosX = event.pageX - 5
  4. var tPosY = event.pageY + 20
  5. $('div.tooltip').css({top: tPosY, left: tPosX})
  6. }
  7. $('table.striped td:nth-child(' + column + ')' )
  8. .addClass('clickable')
  9. .click(function() {
  10. // ...Code continues...
  11. })
  12. .mousemove(positionTooltip)
  13. })
Copyright exforsys.com



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:


Sample Code
  1. var showTooltip = function(event) {
  2. $('div.tooltip').remove()
  3. }
Copyright exforsys.com



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:

 


Sample Code
  1. var showTooltip = function(event) {
  2. $('div.tooltip').remove()
  3. var $thisAuthor = $(this).text()
  4. $('</p><br>
  5. <div class="tooltip">Click to highlight all articles
  6. written by ' + $thisAuthor + '</div>
  7. ')
  8. .appendTo('body')
  9. }
Copyright exforsys.com


 


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:


Sample Code
  1. var showTooltip = function(event) {
  2. $('div.tooltip').remove()
  3. var $thisAuthor = $(this).text()
  4. $('</p><br>
  5. <div class="tooltip">Click to highlight all articles
  6. written by ' + $thisAuthor + '</div>
  7. ')
  8. .appendTo('body')
  9. positionTooltip(event)
  10. }
Copyright exforsys.com


 


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:


Sample Code
  1. .tooltip {
  2. position: absolute
  3. z-index: 2
  4. background: #efd
  5. border: 1px solid #ccc
  6. padding: 3px
  7. }
Copyright exforsys.com



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:



Sample Code
  1. var hideTooltip = function() {
  2. $('div.tooltip').remove()
  3. }
Copyright exforsys.com



Next Page: Showing, Hiding and Positioning jQuery Tooltips





 

 

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