Get the first element that the mouse hovered after a MouseLeave event

dom, dom-events, javascript, jquery

Solution

There exists a `relatedTarget` property on the mouse events

The `MouseEvent.relatedTarget` read-only property is the secondary target for the mouse event, if there is one

and for the `mouseleave` it points to the element that was entered.

So you could just do

$('element').mouseleave(function(event){
    // assuming that allowedList holds the array of allowed elements
    if ( allowedList.indexOf( event.relatedTarget ) > -1 ){
        // found
    } else {
        // not found
    }
});

demo at http://jsfiddle.net/gaby/V4pJb/

Problem

Using jQuery/Javascript, how can I check which was the first element that the mouse hovered after a `MouseLeave` event? Basically, I want to check from an array of elements if the mouse is rolling over those elements or not. I'm trying to use the `event.relatedTarget`. Any help?

Original source