Javascript Element Mouse Hover

Checking if Mouse is Hovering Over an Element in JavaScript

This is a memo on how to determine if the mouse is hovering over an element in JavaScript. Get the element with document.querySelector, and use addEventListener's mouseout/mouseover events as shown below. This logs output each time the mouse cursor hovers over or leaves the #main element.

Shou Arisaka
1 min read
Oct 2, 2025

This is a memo on how to determine if the mouse is hovering over an element in JavaScript. Get the element with document.querySelector, and use addEventListener’s mouseout/mouseover events as shown below.

This logs output each time the mouse cursor hovers over or leaves the #main element.

document.querySelector('#main').addEventListener("mouseout", function(){console.log('out')});
document.querySelector('#main').addEventListener("mouseover", function(){console.log('over')});

Share this article

Shou Arisaka Oct 2, 2025

🔗 Copy Links