Scott on Writing

Musings on technical writing...

Rich Tooltips With jQuery

I was recently working on an application and needed the ability to present the user with a list of links when they hovered over a particular line of text. HTML elements include a title attribute that, when set, displays the attribute's value in a small yellow window when the user hovers over the element. Problem is, the tooltip is a text-only interface. There's no way to embed a hyperlink or image or other rich markup into the tooltip.

Since we are using jQuery in this project I spent some time exploring various techniques for implementing rich tooltips in jQuery. Specifically, I needed a solution that met the following criteria:

  • The tooltip would appear near or next to the text that the user was hovering over, and not on some fixed, predefined spot on the screen.
  • I could, from server-side code, specify what elements would display tooltips when hovered over.
  • I could, from server-side code, customize the tooltip message.
  • The tooltip could include any sort of HTML markup I wanted to throw into it - links, images, etc. - and the user could interact with this markup (click on a link, for instance).
  • The tooltip would remain displayed for a specified amount of time (say, 500 milliseconds) after the user moused off of the element that displayed the tooltip.

Unfortunately, I was unable to find a pre-built solution that would meet our needs, so I created my own, which I'd like to share now.

Please keep in mind that I know just enough JavaScript to shoot myself in the foot. Also, my experience with jQuery is limited, to be kind.

In order to display rich tooltips using my approach you need to do three things.

First, add the following CSS to those pages that use the rich tooltips:

.skmTooltipHost
{
 cursor: help;
 border-bottom: dotted 1px brown;
}

.skmTooltipContainer
{
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 3px;
    padding-bottom: 3px;
    display: none;
    position: absolute;
    background-color: #ff9;
    border: solid 1px #333;
    z-index: 999;
}

Those elements that should display a tooltip when hovered over need to use the skmTooltipHost class. The skmTooltipContainer class defines the styles for the tooltip itself.

Next, add the following $(document).ready function to your page (and add a script reference to the appropriate jquery.js file, if you haven't already):

<script type="text/javascript">
  $(document).ready(function() {
     $(".skmTooltipHost").hover(
       function() {
       $(this).append('<div class="skmTooltipContainer">' + $(this).attr('tooltip') + '</div>');
  
       $(this).find('.skmTooltipContainer').css("left", $(this).position().left + 20);
       $(this).find('.skmTooltipContainer').css("top", $(this).position().top + $(this).height());
        $(".skmTooltipContainer").fadeIn(500);
      },
 
       function() {
        $(".skmTooltipContainer").fadeTo(500, 1.0, function() { $(this).remove(); });
       }
     );
  });
</script>

Finally, decorate those HTML elements that you want to display a tooltip with the skmTooltipHost class and add an attribute named tooltip that contains the content to display in the tooltip. For example, imagine I had a document with the following markup:

<p>ASP.NET simplifies the process of creating dynamic web applications!</p>

I could turn add a tooltip to the text “ASP.NET” by adding the additional markup:

<p><span class="skmTooltipHost" tooltip="ASP.NET was created by Microsoft in 2001.">ASP.NET</span> simplifies the process of creating dynamic web applications!</p>

To get rich markup in the tooltip attribute you'll need to escape the <, >, and " characters with &lt;, &gt;, and &quot;, respectively. For instance, we could add a hyperlink in the above tooltip like so:

<p><span class="skmTooltipHost" tooltip="ASP.NET was created by Microsoft in 2001. Learn more at &lt;a target="_blank" href=&quot;http://www.4guysfromrolla.com/&quot;&gt;4GuysFromRolla.com&lt;/a&gt;!">ASP.NET</span> simplifies the process of creating dynamic web applications!</p>

You can see these rich tooltips in action here - http://scottonwriting.net/sowBlog/CodeProjectFiles/JQueryTooltipDemo.htm

And lastly, to do this programmatically from server-side code, you would add a Label Web control to your page named, say, lblASPNET. You'd set its CssClass property to skmTooltipHost. To set its tooltip attribute add the following (untested) code to the Page_Load event handler:

lblASPNET.Attributes.Add("tooltip", Server.HtmlEncode("ASP.NET was created by Microsoft in 2001. Learn more at <a target="_blank" href="http://www.4guysfromrolla.com/">4GuysFromRolla.com</a>!")

posted on Friday, October 23, 2009 3:17 PM

Feedback

#  Scott on Writing Scripts Rss 10/23/2009 11:06 PM Pingback/TrackBack

Scott on Writing Scripts Rss

# re: Rich Tooltips With jQuery 10/24/2009 1:00 AM Andrei Rinea

Very cool! But there is a slight glitch : if the HTML element (text for example) for which the tooltip is provided is too close to the right side the tooltip will be presented clipped.

Probably there needs to be calculated the maximum allowed left and top for the tooltip div.

Great work!

# Dew Drop &#8211; October 24, 2009 | Alvin Ashcraft&#039;s Morning Dew 10/24/2009 3:39 PM Pingback/TrackBack

Dew Drop &#8211; October 24, 2009 | Alvin Ashcraft&#039;s Morning Dew

# re: Rich Tooltips With jQuery 10/24/2009 6:45 PM ms440

Cool! But what do you think about qTip http://craigsworks.com/projects/qtip/ ?

# 
Twitter Trackbacks for

Scott on Writing
[scottonwriting.net]
on Topsy.com
10/26/2009 8:03 AM Pingback/TrackBack


Twitter Trackbacks for

Scott on Writing
[scottonwriting.net]
on Topsy.com

# re: Rich Tooltips With jQuery 10/29/2009 2:50 PM leonardo pugliese

Great work! Thanks..

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<December 2009>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 2046.8%
Monday 42514.1%
Tuesday 51317.1%
Wednesday 55218.4%
Thursday 57919.3%
Friday 54418.1%
Saturday 1876.2%
Total 3004100.0%

Hour1Total% of Total
12:00 AM 762.5%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 682.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1785.9%
8:00 AM 1926.4%
9:00 AM 1575.2%
10:00 AM 1866.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1685.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1063.5%
6:00 PM 1013.4%
7:00 PM 1063.5%
8:00 PM 923.1%
9:00 PM 872.9%
10:00 PM 893.0%
11:00 PM 953.2%
Total 3004100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.94158
Monday 4.80384
Tuesday 4.10476
Wednesday 7.46679
Thursday 6.24674
Friday 5.02457
Saturday 4.76176
Total 5.413004

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06291
10:00 AM 5.61275
11:00 AM 4.27192
12:00 PM 6.14350
1:00 PM 3.17133
2:00 PM 5.00230
3:00 PM 7.62320
4:00 PM 3.96107
5:00 PM 6.00168
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.58163
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.413004

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles