Tooltips with jQuery

Categories: Featured, JavaScript and jQuery
Written By: admin

This is my first article in english. If you find any errors please let me know so I can practice. Thank you!

That said, let’s come to the article itself. It’s all about a handy tooltip script based on the famous jQuery framework. Below you’ll find some examples of what effect can be achieved using it:

Basic tooltip
Tooltip with multiple lines
Tooltip with title
Forgot to define the tooltip text (nothing should happen)
Tooltip displaying an image (HTML)
Tooltip with some basic HTML (only inline elements)
SPAN element with a tooltip

I know that there are many tooltip scripts out there, but I couldn’t find any that would fit my needs, so I wrote my own. It’s easy to use and implemented within seconds. All you have to do to get it to work is to insert the JavaScript file into your site and define some CSS (feel free to change it’s look and feel)

To be inserted into your site:

<script src="path/to/jqueryTooltip.js" type="text/javascript"><!--mce:1--></script>

CSS:

/* Tooltips */
#tooltip {
	position: absolute;
	padding: 4px;
	color: #FFF;
	font-size: 11px;
	display: none;
	text-align: left;
	background-color: #000;
	opacity: 0.8;
	-moz-opacity: 0.8;
	filter: alpha(opacity=80);
}
 
.tooltipTitle {
	font-weight: bold;
	color: #CEE1EF;
}

You’re already done! Now just give any element you want to show a tooltip the class “tooltip” (this is the default class, change it if you wish in the JavaScript file) and put your tooltip text into the title attribute. Below you’ll find different examples on how to use the script:

Basic tooltip:

<a class="tooltip" title="visit google.com" href="http://www.google.com">Google</a>

» Google

Tooltip with multiple lines:
You can force a line wrap by writing a “::”

<a class="tooltip" title="visit google.com::and find what you're looking for" href="http://www.google.com">Google</a>

» Google

Tooltip with title
You can define the title by using a BB-Code like Syntax: [!]Text[/!]

<a class="tooltip" title="[!]Important[/!]visit google.com::and find what you're looking for" href="http://www.google.com">Google</a>

» Google

Tooltip showing an image
If you want to show an image just use HTML, but replace the “<” and “>” by “[" and "]”

<a class="tooltip" title="[img src='http://the-godking.com/wp-content/uploads/2008/08/jquerytooltip.jpg' /]" href="http://www.google.com">Google</a>

» Google

Tooltip showing basic HTML
If you want to use HTML within you tooltip, just use it, but replace “<” and “>” by “[" and "]“. Please note, that only inline elements are supported.

<a class="tooltip" style="text-decoration:underline;" title="[!]Important[/!]visit [strong]google.com[/strong]::and [em]find what you're [span style=" href="http://www.google.com">Google</a>

» Google

Tooltip on any element:
You can put tooltips on (almost) any element you wish.

<span class="tooltip" title="Google is a search engine">Google</span>

» Google

Feel free to use the script on your website. If you have any questions concerning it just ask (please use the comment form below). Have fun!

12 Responses to “Tooltips with jQuery”

  1. Amir Says:

    hi, Tnx for the great work, it’s been a long time i’m searching for an easy to implement and universal tooltip based on jQuery, and I found yours, and let me tell you, BRAVO!

    Just one thing, I’m trying to implement it to my new theme which will be online shortly, but the problem is that the tooltip doesn’t show everytime, and time to time, i get a “undefined” message inside the tooltip, I’m using it on a tag.
    Please help.

  2. admin Says:

    Hey Amir,
    I just updated the script. Wrapping the whole thing with “jQuery(document).ready(function() { … });” does the job. Now the undefined error should’nt show up any longer ;)

    All you need to do ist to download the new version (or of course wrap your version with the above code) and there you go ;)

  3. Kev Says:

    I too was looking for a simple implimentation of this to get me going, and this is it!

    The image in the image example isn’t working though - hope I can get it working for me.

    Thanks for this.

  4. admin Says:

    @Kev: You were right. It actually works with images but the path was broken. I fixed it.

  5. WebCobra Says:

    Hey can you help please this script doesn’t work for me? All I get is a link and when I hover over it then I just get the title text.

  6. admin Says:

    @WebCobra: If you could post an example of your code that isn’t working I’m sure I could help you out ;)

  7. berlin Says:

    Gut!

  8. christian Says:

    Hallo ich habe auch das Problem wie WebCobra, dass der Code bei mir leider nicht geht. Habe hier den Code:
    [code]

    #tooltip {
    background:#000000 none repeat scroll 0 0;
    color:#FFFFFF;
    display:none;
    font-size:11px;
    opacity:0.8;
    padding:4px;
    position:absolute;
    text-align:left;
    }
    .tooltipTitle {
    color:#CEE1EF;
    font-weight:bold;
    }

    Google

    [/code]
    Die Pfade zu den Javascripten stimmen auch.
    Wäre nett wenn Du mir auch helfen könntest
    Viele Grüße Christian

  9. admin Says:

    Hi christian,

    Du musst deinem Link die Klasse “tooltip” zuordnen. Siehe Beispiele oben. (class=”tooltip”)

    Hoffe das bringt dich weiter.

    Grüsse - Robert

  10. thinkers Says:

    Hi!

    can you plz send me a simple example of you code with all the files needed.

    Thanks

  11. Christoph Burgdorfer Says:

    doesn’t work. Gives an error:

    tooltip is not defined

    source File: jqueryTooltip.js

  12. Christoph Burgdorfer Says:

    PS: That’s my code:

    1.) JS file is an exact copy of http://the-godking.com/wp-content/demo/jqueryTooltip/jqueryTooltip.js

    2.) Inclusion in header is:

    3.) CSS is also an exact copy:
    #tooltip {
    position: absolute;
    padding: 4px;
    color: #FFF;
    font-size: 11px;
    display: none;
    text-align: left;
    background-color: #000;
    opacity: 0.8;
    -moz-opacity: 0.8;
    filter: alpha(opacity=80);
    }

    .tooltipTitle {
    font-weight: bold;
    color: #CEE1EF;
    }

    Link is:

    Help!

    Error is:

    tooltip is not defined

    source File: jqueryTooltip.js

Leave a Reply

Featured & Popular Articles