javascriptjquerydomhoverjquery-hover

Accessing an element that is currently being hovered by mouse, from anywhere in the document


Is there any way that I can access an element that currently has mouse hovered over it using jquery?

I know that I can access an element by attaching mouseover event to an element, but how about when I haven't applied any event to an element and want to access that specific element which is being hovered over by the mouse from any where in the document?


Solution

  • You can attach the mouseover event to the document and get the current element being hovered by using event.target.

    Try,

    $(document).mouseover(function(e){
      console.log($(e.target).attr('id')); // i just retrieved the id for a demo
    });