How to add a tooltip to a div using JavaScript?

To add a tooltip to a div using JavaScript, first create a function that will generate the tooltip content. Next, add an event listener to the div that will call the function and display the tooltip when the div is hovered over. Finally, use CSS to style the tooltip and position it appropriately.

A tooltip is a small text box that appears when a user hovers over a specific element on a webpage, such as a button, link, or image. The tooltip typically contains additional information or context about the element that the user is hovering over. Tooltips are commonly used in user interfaces to provide users with additional information about a specific element without cluttering the main interface.

Method 1: Using CSS and JavaScript Events

This approach involves creating HTML structure with a tooltip element and using JavaScript to show/hide it on mouse events.

Step 1: Create HTML Structure

First, create a div element in HTML and give it an id or class name to be able to target it with JavaScript:

<div id="myDiv">This is my div</div>

Next, create a tooltip element, such as a span, and give it a class name:

<div id="myDiv">This is my div 
    <span class="tooltip">This is my tooltip</span>
</div>

Step 2: Add Event Listeners

In JavaScript, use the document.getElementById method to select the div element, and add event listeners for the mouseover and mouseout events:

const myDiv = document.getElementById("myDiv");
myDiv.addEventListener("mouseover", showTooltip);
myDiv.addEventListener("mouseout", hideTooltip);

Step 3: Create Show and Hide Functions

Within the showTooltip function, select the tooltip element and set its display property to "block":

function showTooltip() {
    const tooltip = document.querySelector(".tooltip");
    tooltip.style.display = "block";
}

function hideTooltip() {
    const tooltip = document.querySelector(".tooltip");
    tooltip.style.display = "none";
}

Step 4: Add CSS Styling

.tooltip {
    display: none;
    background-color: #333;
    color: white;
    padding: 5px;
    border-radius: 4px;
    position: absolute;
    z-index: 1000;
}

Complete Example

Here is a complete working example combining HTML, CSS, and JavaScript:

<!DOCTYPE html>
<html>
<head>
    <style>
        .tooltip {
            display: none;
            background-color: #333;
            color: white;
            padding: 8px;
            border-radius: 4px;
            position: absolute;
            z-index: 1000;
            font-size: 12px;
            white-space: nowrap;
        }
        
        #myDiv {
            background-color: #f0f0f0;
            padding: 20px;
            margin: 20px;
            border: 1px solid #ccc;
            cursor: pointer;
            position: relative;
        }
    </style>
    <title>Tooltip Example</title>
</head>
<body>
    <div id="myDiv">
        Hover over this div to see tooltip
        <span class="tooltip">This is a helpful tooltip!</span>
    </div>
    
    <script>
        const myDiv = document.getElementById("myDiv");
        
        myDiv.addEventListener("mouseover", showTooltip);
        myDiv.addEventListener("mouseout", hideTooltip);
        
        function showTooltip() {
            const tooltip = document.querySelector(".tooltip");
            tooltip.style.display = "block";
        }
        
        function hideTooltip() {
            const tooltip = document.querySelector(".tooltip");
            tooltip.style.display = "none";
        }
    </script>
</body>
</html>

Method 2: Dynamic Tooltip Creation

This approach creates tooltips dynamically using JavaScript without pre-existing HTML tooltip elements:

<!DOCTYPE html>
<html>
<head>
    <style>
        .dynamic-tooltip {
            background-color: #333;
            color: white;
            padding: 8px;
            border-radius: 4px;
            position: absolute;
            z-index: 1000;
            font-size: 12px;
            white-space: nowrap;
            pointer-events: none;
        }
        
        .hover-div {
            background-color: lightblue;
            padding: 20px;
            margin: 20px;
            border: 1px solid #333;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="hover-div" data-tooltip="Dynamic tooltip text">
        Hover for dynamic tooltip
    </div>
    
    <script>
        const divs = document.querySelectorAll('[data-tooltip]');
        
        divs.forEach(div => {
            div.addEventListener('mouseenter', function(e) {
                const tooltip = document.createElement('div');
                tooltip.className = 'dynamic-tooltip';
                tooltip.textContent = this.getAttribute('data-tooltip');
                tooltip.style.left = e.pageX + 10 + 'px';
                tooltip.style.top = e.pageY - 30 + 'px';
                document.body.appendChild(tooltip);
                this.tooltip = tooltip;
            });
            
            div.addEventListener('mouseleave', function() {
                if (this.tooltip) {
                    document.body.removeChild(this.tooltip);
                    this.tooltip = null;
                }
            });
        });
    </script>
</body>
</html>

Key Points

  • Positioning: Use position: absolute for proper tooltip placement
  • Z-index: Ensure tooltips appear above other elements with high z-index values
  • Mouse Events: Use mouseenter/mouseleave or mouseover/mouseout
  • Dynamic Creation: Create tooltips on-demand for better performance with many elements

Conclusion

Adding tooltips to divs can be accomplished through CSS styling combined with JavaScript event handling. Choose the static approach for simple tooltips or dynamic creation for more flexible, reusable tooltip systems.

Updated on: 2026-03-15T23:19:00+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements