Your Ad Here

Home » JavaScript »

13

The following code will generate the simple tooltip using Javascript and css. You can find the demo also.

 

The Javascript code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<script language="javascript" type="text/javascript">
/* This tooltip library was created by Anil Kumar  */
function showtip(e,message)

{var x=0;

var y=0;

var m;

var h;

if(!e)
var e=window.event;

if(e.pageX||e.pageY) { x=e.pageX;  y=e.pageY;  }

else if(e.clientX||e.clientY)

{ x=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;y=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;}
m=document.getElementById('mktipmsg');

if((y>10)&&(y<450))

{  m.style.top=y-4+"px";  }

else{  m.style.top=y+4+"px";  }

var messageHeigth=(message.length/20)*10+25;

if((e.clientY+messageHeigth)>510)

{  m.style.top=y-messageHeigth+"px"; }
if(x<850) { m.style.left=x+20+"px";  }

else{  m.style.left=x-170+"px";  }

m.innerHTML=message;m.style.display="block";m.style.zIndex=203;

}

function hidetip(){

var m;

m=document.getElementById('mktipmsg');m.style.display="none";

}

</script>

The CSS Code:

1
2
3
4
<style type="text/css">

#mktipmsg {padding: 5px; background-color: #FFF8DC;  border: 1px solid #DEB887; width:180px;font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #6b6b6b; display:none; position:absolute;left:0px;top:0px; }
</style>
1
2
3
4
<a href="javascript:void(0)" onmouseover="showtip(event, 'Simple tooltip text');"
onmouseout="hidetip();">Hover your mouse here to see simple tooltip text.</a>

<div id="mktipmsg" ></div>

It will be useful …


You may like these posts

    How to build virtual keyboard for your web applicationSteps to implement and hosting your website in google app engine for freeHow to request a cross domain ajax with in the js fileTo disable the right click on the webpage?

 

13 Comments

  1. Shary says:

    This code is not working, Kindly can you tell me what is problem, I am using this code:

    ToolTip

    .mktipmsg {padding: 5px; background-color: #FFF8DC; border: 1px solid #DEB887; width:180px;font-family: Arial,

    Helvetica, sans-serif; font-size: 12px; color: #6b6b6b; display:none; position:absolute;left:0px;top:0px; }

    function showtip(e,message){var x=0;var y=0;var m;var h;if(!e)
    var e=window.event;if(e.pageX||e.pageY){x=e.pageX;y=e.pageY;}
    else

    if(e.clientX||e.clientY){x=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;y=e.clientY+document.body.s

    crollTop+document.documentElement.scrollTop;}
    m=document.getElementById(’mktipmsg’);if((y>10)&&(y510)
    {m.style.top=y-messageHeigth+”px”;}
    if(x<850){m.style.left=x+20+”px”;}
    else{m.style.left=x-170+”px”;}
    m.innerHTML=message;m.style.display=”block”;m.style.zIndex=203;}
    function hidetip(){var m;m=document.getElementById(’mktipmsg’);m.style.display=”none”;}

    Hover your mouse here to see simple tooltip text.

    Please help me in correcting this Script

  2. Try to write the javascript code line by line then we know that condition from where to where, there is problem of missing closing brackets in your script.

  3. shary says:

    Tooltip

    /* This tooltip library was created by Anil Kumar */

    function showtip(e,message)

    {
    var x=0;

    var y=0;

    var m;

    var h;

    if(!e)
    {
    var e=window.event;
    }

    if(e.pageX || e.pageY) { x=e.pageX; y=e.pageY; }

    else if(e.clientX || e.clientY)
    {
    x=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
    y=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
    }
    m=document.getElementById(‘mktipmsg’);

    if ((y>10)&&(y510)
    {
    m.style.top=y-messageHeigth+”px”;
    }
    if(x<850)
    {
    m.style.left=x+20+"px";
    }

    else
    {
    m.style.left=x-170+"px";
    }

    m.innerHTML=message;
    m.style.display="block";
    m.style.zIndex=203;

    }

    function hidetip(){

    var m;

    m=document.getElementById('mktipmsg');
    m.style.display="none";

    }

    #mktipmsg
    {
    padding: 5px;
    background-color: #FFF8DC;
    border: 1px solid #DEB887;
    width:180px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #6b6b6b;

    position:absolute;
    left:0px;
    top:0px;
    }

    Hover your mouse here to see simple tooltip text.

    Please help me, This Script is not working, i use your Script that you posted above, and i am using Mozilla FireFox 3.5….
    Waiting for your reply.
    Thanks

  4. shary says:

    Tooltip

    /* This tooltip library was created by Anil Kumar */

    function showtip(e,message)

    {
    var x=0;

    var y=0;

    var m;

    var h;

    if(!e)
    {
    var e=window.event;
    }

    if(e.pageX || e.pageY) { x=e.pageX; y=e.pageY; }

    else if(e.clientX || e.clientY)
    {
    x=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
    y=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
    }
    m=document.getElementById(‘mktipmsg’);

    if ((y>10)&&(y510)
    {
    m.style.top=y-messageHeigth+”px”;
    }
    if(x<850)
    {
    m.style.left=x+20+"px";
    }

    else
    {
    m.style.left=x-170+"px";
    }

    m.innerHTML=message;
    m.style.display="block";
    m.style.zIndex=203;

    }

    function hidetip(){

    var m;

    m=document.getElementById('mktipmsg');
    m.style.display="none";

    }

    #mktipmsg
    {
    padding: 5px;
    background-color: #FFF8DC;
    border: 1px solid #DEB887;
    width:180px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #6b6b6b;

    position:absolute;
    left:0px;
    top:0px;
    }

    Hover your mouse here to see simple tooltip text.

    Please help me, This Script is not working, i use your Script that you posted above, and i am using Mozilla FireFox 3.5….
    Waiting for your reply…..
    Thanks

  5. Write the below code

    messageHeigth=-10;
    if((y>10)&&(y<510))
    {
    m.style.top=y-messageHeigth+"px";
    }
    then we can get the tooltip text, i am also using firefox 3.5 only, it is working fine now.

    see the live demo @ http://labs.anil2u.info/tooltip.php

  6. Shary says:

    Thanks,Anil, it working fine now.

  7. Shary says:

    Hi, Anil,

    Can you tell me how i can made this Tooltip moving with the Movement of Mouse,
    Please guide me in this work, Waiting for your response.
    Thanks

  8. Hi Shary,

    I am also not tried with that requirement.

  9. shary says:

    Hi, i Just got the solution,

    The Solution is, Go to HTML part and change this line

    onmouseover=”showtip(event,’message’);”

    with below line

    onmousemove=”showtip(event,’message’);”

    You will get sliding Tooltip with the movement of your Mouse.

    Thanks

  10. Hi shary,

    thanks you very much for your solution,

  11. Prathap says:

    Hello ANil,

    Whenever a text box gets a focus , a tooltip will be displayed at the RHS of the text box . I would like to know how to achieve that

    Please help me

  12. Hello Pratap,

    If adjust the x and y positions then above code will works fine. Or once check the URL : http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx

  13. gopi says:

    Hi,

    It’s very nice post yaar………. thank you so much………. it’s working really fantastic.. I implemented in my application…..

Leave a Reply