Determine which element the mouse pointer is on top of in Javascript

I want a function that tells me which element the mouse cursor is over.

So, for example, if the user's mouse is over this textarea (with id wmd-input), calling window.which_element_is_the_mouse_on() will be functionally equivalent to $("#wmd-input")



In newer browsers, you could do the following:

document.querySelectorAll( ":hover" );

That'll give you a NodeList of items that the mouse is currently over in document order. The last element in the NodeList is the most specific, each preceding one should be a parent, grandparent, and so on.


Although the following may not actually answering the question, since this is the first result of googling (the googler may not asking exactly the same question:), hope it will provide some extra input.

There are actually two different approaches to get a list of all elements the mouse is currently over (for newer browsers, perhaps):

The "structual" approach - Ascending DOM tree

As in dherman's answer, one can call

var elements = document.querySelectorAll(':hover');

However, this assumes that only children will overlay their ancestors, which is usually the case, but not true in general, especially when dealing with SVG where element in different branches of the DOM tree may overlap each other.

The "visual" approach - Based on "visual" overlapping

This method uses document.elementFromPoint(x, y) to find the topmost element, temporarily hide it (since we recover it immediately in the same context, the browser will not actually renders this), then go on to find the second topmost element... Looks a little hacky, but it returns what you expect when there are, e.g., siblings elements in a tree occluding each other. Please find this post for more details,

function allElementsFromPoint(x, y) {
    var element, elements = [];
    var old_visibility = [];
    while (true) {
        element = document.elementFromPoint(x, y);
        if (!element || element === document.documentElement) {
        old_visibility.push(; = 'hidden'; // Temporarily hide the element (without changing the layout)
    for (var k = 0; k < elements.length; k++) {
        elements[k].style.visibility = old_visibility[k];
    return elements;

Try both, and check their different returns.


elementFromPoint() gets only the first element in DOM tree. This is mostly NOT enough for developers needs. Therefore there ist this nice function:

document.elementsFromPoint(x, y) . // mind the 's'

This returns an array of all element objects under the given points.

For older browsers which is not supported this, you may use this answer as a fallback

More info on older browser compatibility here:


Mouseover events bubble, so you can put a single listener on the body and wait for them to bubble up, then grab the or event.srcElement:

function getTarget(event) {
    var el = || event.srcElement;
    return el.nodeType == 1? el : el.parentNode;

<body onmouseover="doSomething(getTarget(event));">
<!-- One simple solution to your problem could be like this: -->

<input type="text" id="fname" onmousemove="javascript: alert(;" />
<!-- OR -->
<input type="text" id="fname" onclick="javascript: alert(;" />
<!-- Both mousemove over the field & click on the field displays "fname"-->
<!-- Works fantastic in IE, FireFox, Chrome, Opera. -->
<!-- I didn't test it for Safari. -->

DEMO :D Move your mouse in the snippet window :D

document.addEventListener('mouseover', function (e) {
	  console.log ("You are in ",;


The target of the mousemove DOM event is the top-most DOM element under the cursor when the mouse moves:

    //Don't fire multiple times in a row for the same element
    var prevTarget=null;
    document.addEventListener('mousemove', function(e) {
        //This will be the top-most DOM element under cursor

This is similar to @Philip Walton's solution, but doesn't require jQuery or a setInterval.


Let me start out by saying that I don't recommend using the method I'm about to suggest. It's much better to use event driven development and bind events only to the elements you're interested in knowing whether or not the mouse is over with mouseover, mouseout, mouseenter, mouseleave, etc.

If you absolutely MUST have the ability to know which element the mouse is over, you'd need to write a function that binds the mouseover event to everything in the DOM, and then store whatever the current element is in some variable.

You could so something like this:

window.which_element_is_the_mouse_on = (function() {

    var currentElement;

    $("body *").on('mouseover', function(e) {
        if( === e.currentTarget) {
            currentElement = this;

    return function() {

Basically, I've created an immediate function which sets the event on all elements and stores the current element within the closure to minimize your footprint.

Here's a working demo that calls window.which_element_is_the_mouse_on every second and logs what element the mouse is currently over to the console.


Old question but heres a solution for those that may still be struggling. You want to add a mouseover event on the 'parent' element of the child element(s) you want detected. The below code shows you how to go about it.

const wrapper = document.getElementById('wrapper') //parent element
const position = document.getElementById("displaySelection")

wrapper.addEventListener('mousemove', function(e) {
  let elementPointed = document.elementFromPoint(e.clientX, e.clientY)





There's a really cool function called document.elementFromPoint which does what it sounds like.

What we need is to find the x and y coords of the mouse and then call it using those values:

var x = event.clientX, y = event.clientY,
    elementMouseIsOver = document.elementFromPoint(x, y);


jQuery event object


The following code will help you to get element of mouse pointer. Resulted elements will display in console.

document.addEventListener('mousemove', function(e) {
    console.log(document.elementFromPoint(e.pageX, e.pageY)); 

You can look at the target of the mouseover event on some suitable ancestor:

var currentElement = null;

document.addEventListener('mouseover', function (e) {
    currentElement =;

Here’s a demo.


You can use this selector to undermouse object and than manipulate him as jquery object. $(':hover').last();


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.