liljoshu liljoshu - 5 months ago 7x
HTML Question

What's a pure JS (no libraries, no altering html) way to get whatever div mouse is over?

I've tried a few different methods (one that involved getSelection that didn't quite work, another involving lot of inserted code that turned out to be buggy as a termite mound). For a script I'm writing, I need to get whatever div the mouse is currently hovering over (preferably without having to add event listeners to every single div in the entire page.)

Notes: This is not asking if the mouse is over a specific div, or getting the XY position of an element. I can get the XY coordinates of the mouse. So either a direct mouse-to-div or a XY-coordinates-to-div would suffice.


You just need one event listener:

window.addEventListener('mousemove', function(e) {
    //          ^^^^^^^^ your desired element is here.