Important alert: (current site time 7/15/2013 1:42:09 PM EDT)
 

article

CoolMints' ToolTips

Email
Submitted on: 4/9/2004 12:22:25 PM
By: CoolMints 
Level: Beginner
User Rating: By 5 Users
Compatibility: JavaScript
Views: 9719
author picture
(About the author)
 
     Why is this code special? Because it is compact, cross-browser compatible, and lighting fast to implement and to change. Do you want a tooltip for an element? Just add an extra attribute.

This article has accompanying files

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

What?

This tooltips do not function like the usual anoying boxes following your mouse pointer. When you hover over an element, information is shown in single, customisable spot on the page. Every element can have it's tooltip. It only takes one function plus a single line. The script is compatible and tested with Internet Explorer 6.0 and Netscape 7.1. Presumably, it will work on some older versions too.

How to implement?

  1. Paste the script into the head section.

    <script language="javascript" type="text/javascript">
      function displaytip(){
        o=(o=window.event || arguments.callee.arguments[0]).srcElement || o.target;
        j=o.attributes["tiptext"];
        if(!j && o.parentNode.attributes) j=o.parentNode.attributes["tiptext"];
        document.getElementById("tooltips").innerHTML=(j?j.nodeValue:"");
      }
      window.onload = new Function("document.body.onmouseover=displaytip");
    </script>
  2. Make an element with id tooltips.
  3. Add a tiptext attribute to the elements you want to have a tooltip.

How does it work?

If the mouse is moved, the displaytip function checks if the underlaying element has a tiptext attribute. (If not, it tries using the tiptext attribute of the parent element. For example: a paragraph containing a link.) The value of this attribute is set as innerHTML of the tooltips element. If no tiptext has been found, the innerHTML is emptied.

Why this unconventional code writing style?

To make the code as compact as possible, without having a messy code. A brief explanation:

o=(o=window.event || arguments.callee.arguments[0]).srcElement || o.target;
This duality is because Netscape does not have the window.event object. I adapted this trick from http://www.javascriptkit.com/dhtmltutors/nsevents.shtml. First, the event object is stored in o. Then, the object that has fired the event is stored, again in o. In IE, this is srcElement; in Netscape, this is target.

j=o.attributes["tiptext"];
if(!j) j=o.parentNode.attributes["tiptext"];

The attribute tiptext is stored in j. If this attribute doens't exist, the tiptext attribute of the parent element is stored in j.

document.getElementById("tooltips").innerHTML=(j?j.nodeValue:"");
The innerHTML of the tooltips element is set to the value of the tiptext attribute, if present. Otherwise, the nodeValue element is cleared.

window.onload = new Function("document.body.onmouseover=displaytip");
This line assigns the displaytip function to the onmouseover event of the body. The construction I used makes it possible to put the code in the head part of your document, as I like to have all my scripts there.

Have fun, and if you like it: vote for this code!

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
4/26/2004 5:00:19 AMRde

Very cool code coolmints. Cross-browser and slim, I like :) Maybe it is really 'status text' rather than 'tooltips', but I get the relationship. 5 from me for this, happy js coding, Rd.
(If this comment was disrespectful, please report it.)

 
4/26/2004 2:17:49 PMCoolMints

Thanks! Maybe you're right about the name, but it was the best I could think of... It had to have a name anyway.
I'm working on some various java scripts, also server side (asp), so expect more soon!
(If this comment was disrespectful, please report it.)

 
5/2/2004 12:34:07 PMCoolMints

Thanks for voting everyone!
I've won the contest prize!
(If this comment was disrespectful, please report it.)

 
5/5/2004 1:09:58 AMRde

And well deserved too.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.