Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » JavaScript » A simple Tooltip with Images and Text

A simple Tooltip with Images and Text

A simple Tooltip with Images and Text.

Page Views : 57382
Downloads : 1242
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
tooltip.zip
 
 
Nevron Chart
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

As a web programmer we need to show tooltip often in our pages but not in an old fashioned way. Something appealing and subtle to the page layout and design.

 

Mostly we go for available open source scripts which are free. I was also doing the same until I decided to write a simple one when I were in need of customizing a free snippet, which took more time.

 

Writing a tool tip is quite simple. This is a simple Tooltip for you web pages with minimal code. Images, Text and HTML code can be shown inside the tooltip.

 

First we have to define a div which is our tooltip which we are going to use for out tooltip. Using a div will help us to show images, html code etc inside a tooltip. We can define the styles of the tooltip like transparency, font etc.

 

Once the tooltip div is ok, next we have to add some attributes/events to the elements for which we are going to use this tooltip.

 

For example:

 

I want to show a tooltip with text only for a span. Then I've to add the below to the span.

 

  1. onmouseover
  2. onmouseout

These two events are used to show the tooltips while the mouse is over the span and hide the tooltip when the mouse is out of the span.

 

<span onMouseOver="toolTip('simple Tooltip Text here')" onMouseOut="toolTip()" class="Text">Simple Tooltip</span>

 

For tooltip function there are three parameters to be passed. First parameter is mandatory, in which we are going to pass the text to be shown for the tooltip. The second and third are the foreground color and background colors, and are optional. If these two parameters are omitted then the default colors will be used.

 

To show an image inside or show some formatted message with html markup what we can do is, we can first form a HTML string then we have to pass to the tooltip function. That's all. The tooltip will be having the formatted text and image. There is nothing special to code for this. The div tag does everything what we need to show (images, html string etc). 

 

<span onMouseOver="show()" onMouseOut="toolTip()" class="Text">Tooltip with Images and Text</span>

 

For formatting I'm using a separate function show() to avoid confusion while using html string and maintain readablility. Inside show() we can format the string, add text, image etc needed for the tooltip.


function show()

{

s = '<table width="100%" cellspacing="2" cellpadding="0" border="0">';

s += '<tr>

<td>

<img src= "http://www.dotnetheaven.com/App_Themes/CSharp/Images/DNHSiteLogo.gif" border="0"/>

</td>

<td valign="top">DotNet Heaven</td>

</tr>';

s += '<tr>

   <td colspan="2" class="Text">

      <hr/>this is a test for simple tooltip. <br/>

      You can add text and images to the tooltip

   </td>

</tr>';

s += '</table>'

toolTip(s)

}

 

Now everything is ready. We are going to look in to, how the tooltip is being shown in the mouse position.

 

Whenever the mouse is moved inside the document, we are detecting the event using

 

document.onmousemove = moveToMousePos;

 

This will place the tooltip in the mouse's current position. The "moveToMousePos" function will detect the mouse coordinates and set the tooltip div's left and top positions. So the when the mouse is over the span we want to show the tooltip, then the tooltip's display property will be set to block and when out, display property is set to none.


A simple tooltip with text only.

 

 

Figure 1:

 

A tooltip with colors set

 


 

Figure 2:


Tooltip with images and text



 

Figure 3:

 

Note:

 

We can set the font styles and opacity of the tooltip using the css class

 

.toolTip

{

    font-family: Verdana, Arial, Sans-serif, 'Times New Roman';

    font-size: 8pt;       

    filter:alpha(opacity=80);

    -moz-opacity: 0.8;

    opacity: 0.8;

    /* comment the above 3 line if you don't want transparency*/

}

 

We can insert html with tables for better-formatted output/look for the tooltip. But don't include html, body tags.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Kumar S
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
tooltip with images by sai On October 13, 2008
i am displaying images,by using tooltip i need to see the names of images from database.
Reply | Email | Modify 
Re: tooltip with images by Kumar On October 14, 2008
You can get the text from the database first.
As You can see the text am using in the image example. simply use your text(got from the database) instead of the text i've used in the example.


Reply | Email | Modify 
tooltip with images by Sean On October 30, 2008
This could prove to be a fabulous way of encorporating a 'help' system into a web project. Thanks for this very clear example.
Reply | Email | Modify 
simple modification by robert On December 19, 2008
I changed the show function to this: so i could easily change the desired image. http://pastebin.com/f1d953fe6 go there because i couldnt add code in here cuz the site is all f*cked up here
Reply | Email | Modify 
fk by robert On December 19, 2008
man, i thought this was awesome until i viewed it in IE its all jacked up in IE, omg what a disaster.
Reply | Email | Modify 
hrmmph by robert On December 19, 2008
now i see the screenshots above, the first two are screenshots in ie, the last is in firefox... seems the author knew it was all fkd up in ie and decided to cover it up instead of fix it. ill just build my own and stop tryin to save time with other peoples hacks.
Reply | Email | Modify 
link by robert On December 20, 2008
http://www.walterzorn.com/tooltip/tooltip_e.htm this one will work with either browser
Reply | Email | Modify 
good by zia On January 18, 2010
thanks robert
Reply | Email | Modify 
Re: good by guy On April 1, 2010
This is a good example.
But how can we modify the display time? Is it possible ?


Reply | Email | Modify 
Tool Tip Information by Arun On December 15, 2010
Hai Kumar.. It is simply good and its subtle to the page layout and design. Thank you
Reply | Email | Modify 
Many Thanks by Oliver On April 9, 2011
Hi Kumar, thank you for your example. Its really helpful and pretty simple. Oliver
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.