Skip to content

Mouseleave triggered by click

I have an absolutely-positioned div, and I’m trying to keep track of when the mouse moves over it, and when the mouse leaves. Unfortunately clicking on the text in the box occasionally triggers the mouseleave event.

DEMO: js fiddle

How can I prevent this?


let tooltip = document.createElement('div');
tooltip.innerHTML = 'HELLO WORLD';
tooltip.setAttribute('class', 'tooltip'); = 'none';
tooltip.onclick = evt => {
tooltip.ondblclick = evt => {
    console.log('double click')
tooltip.onmouseenter = () => {
    console.log('tooltip mouse OVER');
tooltip.onmouseleave = () => {
    console.log('tooltip mouse OUT')
} = '290px'; = '50px'; = 'block';


<div style="width: 300px; height: 300px; background-color: lightblue">


.tooltip {
    position: absolute;
    /*display: none;*/
    left: 100;
    top: 100;
    min-width: 80px;
    height: auto;
    background: none repeat scroll 0 0 #ffffff;
    border: 1px solid #6F257F;
    padding: 14px;
    text-align: center;


This seems to be a bug (I could reproduce it in Chrome with clicks that have the mouse down and mouse up happening rapidly after each other).

I would suggest to work around this issue by checking whether the mouse is still over the element at the moment the event is fired:

tooltip.onmouseleave = (e) => {
    if (tooltip === document.elementFromPoint(e.clientX, e.clientY)) {
        console.log('false positive');
    console.log('tooltip mouse OUT')

The downside is that when the browser window loses focus, that is also considered a false positive. If that is an issue for you, then check this answer.