sawa
sawa

Reputation: 168239

How to get the DOM object

How can I get the (innermost) DOM object that the mouse cursor points to? Unlike other events, a keyup event's e.target returns only a rough object like html body. I want to know the innermost DOM object on keyup events, just like what is returned by e.target of a click event.

Upvotes: 0

Views: 150

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 186063

Here:

document.body.onmousemove = function ( e ) {
    var topmost = document.elementFromPoint( e.clientX, e.clientY );
};

Live demo: http://jsfiddle.net/yVtsn/1/

Upvotes: 3

qwertymk
qwertymk

Reputation: 35304

If you're open to jQuery...

DEMO

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);

document.elementFromPoint

jQuery event object

Upvotes: 3

Related Questions