Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an element with mousemove

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

like image 265
thomas-hiron Avatar asked Dec 01 '12 11:12

thomas-hiron


1 Answers

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.

like image 178
T.J. Crowder Avatar answered Sep 23 '22 19:09

T.J. Crowder