Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get what element the mouse is over with the mousemove event on the body

If you use the mousemove event on the body tag. Is it possible to get what element in the html the mouse is currently over.

$('body').mousemove(function (e) {

var details = e; // can e.something return what element the mouse cursor is over?

console.log(details);

});
like image 653
Hello-World Avatar asked May 15 '13 09:05

Hello-World


People also ask

How do you check if the mouse is over an element?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery.

Which event is used when mouse moves over the element?

The mouseover event is fired at an Element when a pointing device (such as a mouse or trackpad) is used to move the cursor onto the element or one of its child elements.

How does Mousemove work?

MouseMove is a simple event that is executed when a pointer is moving over or around an element. Mousemove is a javascript event that inside a web page. The mousemove event can also be understood as a part of an event handler, whereupon some action or movement by a mouse pointer, an intended script is executed.


2 Answers

Use e.target. For more information, you can check event.target documentation.

$('body').mousemove(function (e) {
     var details = e.target; // can e.something return what element the mouse cursor is over?
     console.log(details);
});

Here is the demo : http://jsfiddle.net/PaX7b/1/

like image 117
Nishu Tayal Avatar answered Nov 07 '22 15:11

Nishu Tayal


You can use event.target

for getting id use

var id = event.target.id;

use can also check using this

 var $target = $(event.target);
 if ($target.is("a")) {
        }
like image 33
Harshit Tailor Avatar answered Nov 07 '22 16:11

Harshit Tailor