Friday, December 5, 2008

JavaScript ToolTip

Today i will discuss tooltip in javascript how can you create it and use it very quickly in your code. I have seen that there is no such control for tooltip in, so the developer try to design there own tooltip. Some of the control in the has the property tooltip but not all has. So in order to show the tooltop for those control here is the little javascript code to use it. This Sample Javascript code is taken from the Itookia, and I have modified the code according to my need and also the i have use the stylesheet for the formatting.

and the tooltip which contain the help information .

and tooltip which contain the image.

The Css used for this tooltip which i have changed is takend from Itookia



filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,

StartColorStr=#ffffff, EndColorStr=#cecfde);

cursor: hand;

width: 300px;

font-size: 11px;

padding-left: 5px;

padding-right: 5px;

border: 1px solid #7b9ebd;

position: absolute;

visibility: hidden;




padding: 6px 0 4px 10px;

font: bold 12px Trebuchet MS ,Arial;

float: left;

clear: both;

width: 100%;




font: normal 12px Trebuchet MS, Arial;

padding: 0 2px 6px 20px;

margin: 0;

float: left;




font: bold 12px Trebuchet MS ,Arial;

margin-right: 10px;

border: #BDBDBD 1px solid;

float: left;


The javascript code which i have modified is

var lastTooltip =null;

showTooltip = function (strTitle,strText,strHelpText,strhelpImage,strmainImage)


if (lastTooltip==null)


var newDiv = document.createElement("div");


var title = document.createElement("span");


var titleText = document.createTextNode(strTitle);


var mainParagraph = document.createElement("p");


if (strmainImage)


var mainImg = document.createElement("img");



mainParagraph.appendChild(mainImg); }

var mainText = document.createTextNode(strText);




if (strHelpText)


var horLine = document.createElement("hr");'96%';'both';

var helpDiv = document.createElement("div");'left';'left';'both';'6px';'24px';

if (strhelpImage)


var helpImg = document.createElement("img");




var helpText = document.createTextNode(strHelpText);






if (document.addEventListener)

document.addEventListener("mousemove",moveTooltip, true);

if (document.attachEvent)


var bodyRef = document.getElementsByTagName("body").item(0);




moveTooltip = function (e)




if (document.all)

e = event;

if (

sourceEl =;

else if (e.srcElement)

sourceEl = e.srcElement;

var coors=findPos(sourceEl);

var positionLeft = e.clientX;

var positionTop = coors[1] + sourceEl.clientHeight + 1;'px';'px';'visible';



hideTooltip = function ()


var bodyRef = document.getElementsByTagName("body").item(0);

if (lastTooltip)




function findPos(obj)


var curleft = curtop = 0;

if (obj.offsetParent)


curleft = obj.offsetLeft

curtop = obj.offsetTop

while (obj = obj.offsetParent)


curleft += obj.offsetLeft curtop += obj.offsetTop



return [curleft,curtop];


and the javascript function calls for all the above image show are

onmouseover="showTooltip('Simple tooltip','This is simple tooltip')"


onmouseover="showTooltip('Simpe tooltip with additional info','This is simple tooltip with additional help info.','Press F1 for more help.','help.png','')"
= "hideTooltip()"

onmouseover="showTooltip('Complex tooltip','This is complex tooltip with image.','Press F1 for more help.','help.png','graf.png')"


Hope you will like this post for the tooltip in using javascript :)
you can now download source code from here


TasleemArif said...

good, few days ago i have gone/read such a tooltip of java script,but it was not from itookia :) nice effort with bitmap support as well.

awaisj said...

nice post. is it possible to provide a download link to a zip of all the required files?

Asim said...

Awais: Thanks for you comments, i am new to this field , i mean in blogging, and i have search alot how to upload the zip files for download, but couldn't find, if you have any idea about it or anyone i will be thankful to him/you

Asim said...

Awais: Sorry for above reply now i have give link to download the code, you can know download and see the project.hope this will help you more now

wasim said...

Asim good work. Keep it up