A lightweight prototype based JavaScript tooltip

Posted by Jonathan

For a recent project I needed JavaScript tooltip functionality for showing detail information. All tooltip libraries that I came across were too complicated and bloatet, did just too much and most of the time were still not flexible enough with the tooltip. So I decided to create my own library that is based on prototype.js:

<script src="/javascripts/prototype.js" type="text/javascript"></script>
<script src="/javascripts/tooltip.js" type="text/javascript"></script>

<div id='tooltip' style="display:none; margin: 5px; background-color: red">
  Detail infos on product 1....<br />
</div>

<div id='product_1'>
  This is product 1
</div>

<script type="text/javascript">
  var my_tooltip = new Tooltip('product_1', 'tooltip')
</script>

Now whenever you trigger a mouseOver on the `trigger` element, the tooltip element will be shown slightly below the mouse pointer. On the mouseOut event the tooltip disappears. The script is clever enough to move the tooltip to the top and/or left if there is not enough space left on the screen to display the tooltip.

This way you are totally flexible with the tooltip. It can be any div with any CSS you like.

You can use my_tooltip.destroy() to remove the event observers and thereby the tooltip.

At the moment the tooltip appears and hides with Element.show() and Element.hide() but a future version will use the script.aculo.us effects.

Download it here (BSD license): tooltip-v0.1.js

UPDATE:
I've put up an online demo here. In the future more updates can be found here.

Comments

Leave a response

  1. Mark HoltonOctober 20, 2006 @ 04:11 AM
    I really like the weight of this (or lack therefore) -- many thanks!
  2. AndreasOctober 20, 2006 @ 01:13 PM
    Besten Dank! Ein Life-Bsp wäre noch nett.. lightweigth trifft's aber nur, wenn men eh prototype einsetzt - ansonsten sind die beiden scripts zusammen (unkomprimiert) ca 56 kb. Allerdings sehr einfach in der Anwendung.
  3. JaVOctober 20, 2006 @ 05:31 PM
    Cool. How do you make it stick?
  4. JonathanOctober 20, 2006 @ 10:31 PM
    @JaV: Basically the tooltip is displayed near the mouse on a mouseOver event on the trigger element. The tooltip will only be repositioned on the next mouseOver on the trigger. @Andreas: I will put up a live demo soon.
  5. Ivan MinicOctober 20, 2006 @ 11:40 PM
    Nice, clean, simple!
  6. Shani elharrarOctober 21, 2006 @ 07:10 PM
    Nice, But - isn't there any 'pure' html way to do tooltips? b.t.w , i would put the tooltip div after the 'product' div for accesibility circumstances :)
  7. muelliOctober 23, 2006 @ 08:18 PM
    It appears, that this tooltip only works good with text. A Image as the Trigger-Element doesnt work fine. The attached tooltip the stops moving. Any solutions out there?
  8. JohnKOctober 24, 2006 @ 02:46 PM
    I can't seem to get this working. I get the following error: "this.tool_tip.hide is not a function on line 76" in my javascript console. Not doing anything other than try your example code.
  9. JonathanOctober 25, 2006 @ 02:26 PM
    You need protoype 1.5.0rc1
  10. ChristianOctober 25, 2006 @ 11:46 PM
    I am trying to create many tooltips on a single page and I get the following error when the second one is created: this.tool_tip has no properties I am declaring them programatically as follows: var my_tooltip132 = new Tooltip('view_132', 'tooltip_132'); var my_tooltip131 = new Tooltip('view_131', 'tooltip_131'); . . . Any ideas?
  11. ChristianOctober 25, 2006 @ 11:54 PM
    The first line an error pops up is on this.tool_tip.hide();
  12. JonathanOctober 26, 2006 @ 12:49 AM
    @Christian: Make sure to use Prototype 1.5.0_rc1, you can get it also as part of Scriptaculous 1.6.4. I'm using tooltip.js with upto 30 tooltips on the same page without any problems. I've put a live demo up here http://blog.innerewut.de/files/tooltip/demo.html
  13. Kumar ChetanOctober 27, 2006 @ 12:25 PM
    I will try to make this ajaxified. thanks
  14. talwar_October 28, 2006 @ 09:33 AM
    I was also getting the error on this.tool_tip.hide() So i upgraded to prototype 1.5 rc1. now i get the following: "element has no proerties" - in line 1966 of prototype.js any ideas??
  15. JonathanOctober 28, 2006 @ 11:20 AM
    Does the demo at http://blog.innerewut.de/files/tooltip/demo.html work for you? The `element has no propertes` error in prototype hints that the referenced element does not exist. Make sure to use the tooltip after the referenced DOM is created, either by placing the script in the source after the DOM elements or by using window.onload or so.
  16. talwar_October 29, 2006 @ 04:39 AM
    never mind... i made a much simpler version of my own which works just as well without custom event handlers etc. keep up the good work!