qTip is an excellent jQuery plugin created by Craig Thompson. It is built on top of the jQuery Javascript library and allows web developers to build flexible, well formatted and easy to use tool tips. I decided to use qTip for my latest web app and was able to get up and running pretty quickly. I did run into one issue that I thought was worth pointing out to help you avoid the same sticking point that I encountered with my approach.
My start was pretty simple. First I downloaded the latest copy of the qTip javascript include file and included it in the header of my project. The I copied the HTML found here and included it in my main project template just before the closing BODY tag.
This is where it started to get a little sticky. The simple qTip examples show you how to create static, pre-defined tool tips when you create the Javascript for them in your header. This is fine for static sites, but I wanted to have tool tips that were dynamically populated by my application at run time. It was just easier for me to include the tool tip content in my HTML inline as the page was being built than to include it all as part of one big header section. Here is what I settled on for my HTML code.
1 | <a class="comment"><img src="/mysite/media/img/comment.png" /><div>My Tool Tip</div></a> |
As you can see my strategy here is very simple. I create an anchor tag that contains an image and a div. The image is the display object that the user will hover over to see the tool tip. The DIV contains the actual content of the tool tip. To make sure the div does not appear in-line after the image in the rendered page we add the following CSS.
1 | a.comment div { display: none; } |
This makes sure the div and it’s content are both hidden and do not displace the DOM elements around them. Next I needed to create the Javascript that was going to display the tool tip when the user hovered over the image and hide it when the user moved their mouse off the image. The issue I encountered was not being able to address the anchor tag such that I could include the div content in the tool tip. I searched the qTip forums and eventually came up with the following code.
1 2 3 4 5 6 7 8 9 | $(document).ready (function () { $ ('a.comment').each (function (){ $(this).qtip ({ content: $('div', this).html (), show: 'mouseover', hide: 'mouseout' }); }); }) ; |
This code simply caused the anchor tag with the comment class to be selected each time the user hovered the mouse over the image. The content attribute in the array was populated by the contents of the first div child of the current (this) object which is the instance of the anchor tag with the comment class hovered over by the user. The qTip tool tip displayed then contained the contents of the div.
That’s all there was to it. With this simple setup I was able to generate tool tips that were notably superior to those created by the title attribute of the anchor tag. What you see here is only the tip of the iceberg in terms of the depth of features that qTip offers. If you need a simple and customizable tool tip solution for your web site or web app you should definitely check out qTip!


Thanks! I find the documentation on the qtip site a bit confusing/hard to navigate. I am getting an error when I have an anchor tag inside the tip itself – any ideas?
Thanks for the comment Frank. What version of qTip are you using? I see from the qTip website that version 2 will be released soon. I am loading jQuery dynamically from Google and it seems that the code described in this post has stopped working. I’ll have to sort it out. I will try to help if you can give me more details about what you are doing.
I am using qTip 2 yes, like you say I had a bit of trouble getting this to work with verion 1. Using qTip2 it seems like a class related to qTip is being added to the anchor tags within “a.comment div” which messes with the rendering, that div is no longer hidden on the page and does not pop up in a tip as a result.
Hopefully a stable version of qTip 2 is out soon, but just thought I would mention the problem I’m having, cheers.
I went back and looked at my later effort and it is working with the old version of qTip. In that example I’m using a div inside of the anchor tag, but I’m not including an image as the target of the anchor. It’s just regular text. This seems to work, but the older example does not. The only difference I can see in the above code on my working example is that I had to use the following for the div CSS:
td a.comment div { display: none; }
Other than that it’s the same. I will have to find a reason to give qTip2 a try. I’ll let you know if I figure out why the original one I did with the image isn’t working.