Warning ! this module is no longer supported
( Please read this before considering using it)

Description

Almost all browser support some form of "tooltip" natively, via the "TITLE" attribute of HTML element. However, this feature is inconsistently implemented, and browser limitation make this unpractical or useless depending on what you are looking for. Notably, you can't insert HTML content on those tooltips, and only a few browsers accept multi-line values. This library replaces the default handling of TITLE attributes by a mecanism which solve those issues.

How to use it

Mouse-over tooltips

Those tooltips are activated when the mouse rest of the element area, and they hide automatically when the mouse leaves this area. All tooltips are mouseover by default.

HTML
<span
  title="Simple tooltip message">
  this area
</span>
<span
  title="Tooltip message|Simple tooltip message"
  this area
</span>
UI Mouse over [this area] A [tooltip] with a caption

Focus tooltips (form fields)

In addition to trigger the tooltip visibility with mouse position, you may also choose to display tooltips when the keyboard focus enter a form field; this is a convenient way of guiding end user input. When this tooltip is opened this way, its position is choosen so that it does not cover the field.

HTML
<form mjstooltipmode="focus">
...
</span>
UI
A textfield
A checkbox

List

Choice 1
Choice 2

Complex tooltip messages

If your tooltip message contains HTML tags and is a little more complex than a simple string, you may find unpractical the use of TITLE attribute. The tooltip module offer an indirection mecanism which let you define the tooltip content somewhere as part of your document (and with hidden visibility) so that you can compose the message with your favorite editor, without dealing with awful quoting problems (remember, the TITLE attribute must be quoted, and what you put inside need backslashed-quoting \" or \').

In addition to an easier definition of tooltip content, this feature makes possible to recycle the same message for multiple nodes; this may be very interesting if you deal with large set of data having common tooltips.

1. Defines the tooltip message somewhere (in a hidden SPAN for example)
HTML
<span id="aComplexTooltip" style="visibility:hidden;">
   <p>Here is a Sample long tooltip message, using
   <i>arbitrary</i> HTML content:</p>
<ol>
<li>List item #1</li>
<li>List item #2</li>
</ol> </span>
2. Then, reference the message by its ID via the "#ID" syntax:
HTML
<span title="Complex tooltip|#aComplexTooltip">complex tooltip</span>
UI Mouse over [complex tooltip]

Mouse over [same complex tooltip]

Note: only the body of the tooltip message can be set this way; the title must be entered as part of the TITLE attribute.

Large tooltip messages

By default, the tooltip module do not apply specific size adjustment on tooltip message. This is usually fine when dealing with small messages, but this may be problematic with large messages. Instead of statically sizing tooltip messages via a CSS style, you can define the maximum width that a tooltip can use by setting the "mjsTooltipMaxWidth" attribute; this size is set in pixels (do not use the "px" suffix though).

This attribute is inherited; hence you can set it once for multiple tooltips (at the BODY tag level for example).

HTML
<span mjstooltipmaxwidth=300
   title="Large tooltip|#aLargeTooltip">large tooltip
</span>
UI Mouse over [large tooltip]

Mouse over [large tooltip with 'mjstooltipmaxwidth' attribute]

Styling tooltips

In order to make this toolkit instantly usable, the tooltip module generates hardcoded styles for the tip message which mimic the usual rendering for bubble information in most platform (a pale yellow background and a thin black border). If you want to adjust tooltip rendering, you can instruct the module to attach an arbitrary CSS class to the tooltip, which let you customize everything as you wish by editing a few styles. This is done by specifying the attribute "mjstooltipclass". This attribute is inherited, hence attaching this to the BODY tag will redefine all tooltips of the document.

In order to properly implement styling, you need to know hos the tooltip content is organized. The tooltip module used a few CSS classes for each of its components:

  • [yourclass] is set at the root node of the tooltip
  • [yourclass]_caption is set for the caption (title) of the tooltip, when one is defined
  • [yourclass]_body encloses the content of the tooltip itself
<DIV class="[yourClass]">
  <DIV class="[yourClass]_caption">
     Tooltip caption
  </DIV>
  <DIV class="[yourClass]_body">
     Tooltip content
  </DIV>
</DIV>
Note: in this sample, [yourclass] is the value which you have selected via the "mjstooltipclass" attribute.

Here is an example:

CSS
.MyTooltip
{
background-color: #A0A0FF;
color: black;
padding: 0px;
border: 1px solid black;
}
.MyTooltip_body
{
padding: 10px;
}
.MyTooltip_caption
{
background-color: #000080;
color: #FFFFFF;
font-style: italic;
font-weight: bold;
}
HTML
<span mjstooltipclass="MyTooltip" title="Title|Customized tooltip">
  custom tooltip style
</span>
UI Mouse over [custom tooltip style]

Technical notes

How it works

At initialization time, when the document is loaded, the module goes over all elements having a TITLE attribute, compute the HTML data which will be used when showing the tooltip, attach it to the element, and remove the TITLE attribute so that the browser does not use its own handling of titles. In the meantime, appropriate event handler are associated with elements having a title.

JavaScript API

mjs_setTooltipClass(className)

Defines a CSS class name for tooltips. Note that a declaration at the HTML element level (attribute mjstooltipclass) prevails to the values set via this function.

mjs_setTooltipShadowSize(sz)

Specifies the size of the tooltip area shadow in pixels. By default, a small one pixel shadow is used. Using zero disables the shadow.

mjs_setTooltipMaxWidth(sz)

Specifies the maximum width, in pixels, that a tooltip can use. The attribute 'mjsTooltipMaxWidth' prevails to this global value;