5

Is it possible to get the current element using mousemove ? I'd like to get the element where the mouse is to do specifics things if the mouse isn't on an element x or y.

For exemple :

$(document).mousemove(function(e)
    {
        if(e.xxxx.attr("id") == "elem")
            ...
    });

xxxx is what I'm looking for, and I hope it exists :)

Thanks

thomas-hiron
  • 928
  • 4
  • 21
  • 40

1 Answers1

7

If you mean the element the mouse is over, yes, it's available as the target property of the event object.

$(document).mousemove(function(e)
{
    if (e.target.id == "elem") {
        // ...
    }
});

target is a DOM element, and you can access the id of the element directly from its id property (a reflected property that takes its value from the attribute). If you wanted to do other things with it and wanted access to the jQuery functions, you'd use $(e.target) to get a jQuery wrapper for it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875