JS: How to Create Tooltip

By Xah Lee. Date: . Last updated: .

This page shows you how to do floating tooltip, without using any JavaScript library.

Hover over me to see a example.

Here's the HTML code:

<p><span id="t42310">Hover over me</span></p>

Here's the JavaScript code:

// -*- coding: utf-8 -*-
// © 2013, 2018 Xah Lee, http://xahlee.info/
// creating a tooltip

{

// create tooltip element.
const ttBox = document .createElement("div");

// set style
ttBox.id = "tooltip-id13423";
ttBox.style.visibility = "hidden"; // make it hidden till mouse over
ttBox.style.position = "fixed";
ttBox.style.top = "1ex";
ttBox.style.left = "1ex";
ttBox.style.padding = "0.5em";
ttBox.style.width = "30em";
ttBox.style.borderRadius = "1em";
ttBox.style.border = "solid thin gray";
ttBox.style.backgroundColor = "lightgrey";

// insert into DOM
document .body.appendChild(ttBox);

const ttTurnOn = ((evt) => {
    // get the position of the hover element
    const boundBox = evt.target.getBoundingClientRect();
    const coordX = boundBox.left;
    const coordY = boundBox.top;

    // adjust bubble position
    ttBox.style.left = (coordX + 40).toString() + "px";
    ttBox.style.top = (coordY + 40).toString() + "px";

    // add bubble content. Can include image or link
    ttBox.innerHTML = "Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, «and what is the use of a book,» thought Alice «without pictures or conversation?».";

    // make bubble VISIBLE
    ttBox.style.visibility = "visible";
});

const ttTurnOff = ((evt) => { ttBox.style.visibility = "hidden"; });

const hoverEle = document .getElementById("t42310");
// assign handler
hoverEle.addEventListener("mouseover", ttTurnOn , false);
hoverEle.addEventListener("mouseout", ttTurnOff , false);
document .getElementById("tooltip-id13423") . addEventListener("click", ttTurnOff , false);

}

Here's how it works:

  1. Use JavaScript to create a HTML element. This will be your tooltip box.
  2. Make the tooltip box invisible, by setting CSS visibility:hidden.
  3. Set the tooltip box on its own layer (CSS position:fixed), so that it doesn't interfere with other elements on the page.
  4. Assign function/handler to the “mouse over” and “mouse out” events, for elements that you want tooltip box to appear.
  5. The function to activate the tooltip, will change tooltip's CSS so it's visible (visibility:visible), and also position it near the mouse. Also add whatever content you want to the tooltip.
  6. The function to deactivate the bubble, will change the tooltip's CSS so it's invisible again.

If you have trouble understanding the code, here's some detail on how to do each step:

That's it!

Web Scripting Overview

  1. Browser Window Object, DOM
  2. Intro to Event-Based Programing
  3. JavaScript Load Order

DOM How-To

  1. Basic DOM Methods
  2. Get Element by ID, Tag, Name, Class, CSS
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Attribute Value
  8. Set Attribute Value
  9. Remove Attribute
  10. List/Add/Remove Class Attribute
  11. Add/Remove Event Handler
  12. Navigate DOM Tree
  13. Node Type, Name, Value

DOM HTML GUI

  1. Button
  2. Input Text Field
  3. Password Text Field
  4. Textarea
  5. Radio Button
  6. Checkbox
  7. Selection, Popup Menu
  8. Input Range Slider
  9. Color Picker

Web Scripting Examples

  1. Disable Right Click
  2. document.write
  3. Image Rollover
  4. Pop-up New Window
  5. Digital Clock
  6. Stopwatch
  7. Fade a Element
  8. How to Create Tooltip
  9. Falling Snow Effect
  10. Box Following Scroll

Web Scripting Misc

  1. Web Cookies
  2. Web Storage
  3. DOM: Open URL window.open
  4. Encode URL, Escape String
  5. Browser Info, Navigator Object
  6. Get URL (window.location)
  7. Find Window Width
  8. Find Element Width
  9. WebSocket
  10. Value of “this” in Event Handler
  11. Event Delegation
  12. Get Current Script Tag
  13. insertAfter Element
  14. Randomize List
  15. create Document Fragment
  16. innerHTML, nodeValue, textContent, innerText?
  17. What is Live Object
  18. NodeList vs HTMLCollection
  19. Whitespace Nodes

jQuery

  1. jQuery Basics
  2. jQuery() vs querySelectorAll()
  3. Write JQuery Plugin

node.js

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Object Ref
  5. DOM Scripting
  6. SVG
  7. Blog