event.relatedTarget is an Element property naming the other element in a pointer transition — where the mouse is going on mouseout, or where it came from on mouseover. This tutorial covers the official anchor demo, dropdown menus, direction logging, highlight effects, and comparing with target and currentTarget.
01
Property
event.relatedTarget
02
Returns
Element (DOM)
03
Official
mouseout nodeName
04
mouseout
Entering element
05
mouseover
Leaving element
06
Since 1.1.4
Mouse events
Fundamentals
Introduction
When the pointer moves between elements, the browser fires mouseover and mouseout events. Each event involves two elements: the one receiving the event and the one on the other side of the transition. jQuery exposes the second element as event.relatedTarget.
Unlike coordinate properties, relatedTarget returns a DOM Element — you can read nodeName, wrap it in jQuery, or test whether the pointer moved into a child menu. jQuery has forwarded it since version 1.1.4.
Concept
Understanding event.relatedTarget
This property answers one question: which other element is involved in this pointer transition?
mouseout — relatedTarget is the element the pointer is entering.
mouseover — relatedTarget is the element the pointer is leaving.
Read-only — an Element reference; not a method.
May be null — when the pointer enters or leaves the document window.
💡
Beginner Tip
On mouseout from a link to a nearby DIV, event.target is the link but event.relatedTarget is the DIV — exactly what the official jQuery demo alerts with nodeName.
Foundation
📝 Syntax
Official jQuery API form (since 1.1.4):
jQuery
event.relatedTarget
// typical pattern:
$( "a" ).on( "mouseout", function ( event ) {
if ( event.relatedTarget ) {
console.log( event.relatedTarget.nodeName );
}
});
Parameters
None — relatedTarget is a property, not a function.
Return value
Element — the other DOM element involved in the event, or null when the pointer crosses the document boundary.
Official jQuery API example
jQuery
$( "a" ).on( "mouseout", function ( event ) {
alert( event.relatedTarget.nodeName ); // "DIV"
});
Cheat Sheet
⚡ Quick Reference
Event
relatedTarget means
Common use
mouseout
Element pointer is entering
Keep menu open if moving into panel
mouseover
Element pointer is leaving
Detect where hover came from
mouseenter
Not reliably exposed
Prefer mouseover for relatedTarget
mouseleave
Not reliably exposed
Use contains() on mouseout instead
null
Pointer left document
Always guard before access
Compare
📋 relatedTarget vs target vs currentTarget vs delegateTarget
Four element references on the same jQuery event — different roles in delegation and bubbling.
relatedTarget
event.relatedTarget
Other element in the transition
target
event.target
Deepest element that received the event
currentTarget
event.currentTarget
Element bound to this handler
delegateTarget
event.delegateTarget
Element where .on() was attached (delegation)
Hands-On
Examples Gallery
Example 1 follows the official jQuery mouseout demo. Examples 2–5 cover dropdown menus, direction logging, highlight effects, and a four-way target comparison.
📚 Official jQuery Demo
Alert relatedTarget.nodeName on mouseout from an anchor — pointer moves to a sibling DIV.
Example 1 — Official Demo: mouseout nodeName
Official jQuery demo — link followed by a DIV; mouseout from the link logs the entering element’s tag name.
jQuery
$( "a" ).on( "mouseout", function ( event ) {
if ( event.relatedTarget ) {
$( "#log" ).text( "relatedTarget: " + event.relatedTarget.nodeName );
}
});
Move from link to sibling DIV:
#log shows "relatedTarget: DIV"
How It Works
On mouseout, the browser sets relatedTarget to the element under the pointer after leaving the anchor — the adjacent DIV, matching the official API comment // "DIV".
Example 2 — Dropdown: keep open when relatedTarget is inside menu
Close the menu on mouseout only if the pointer did not move into the dropdown panel.
jQuery
var $menu = $( "#menu" );
$( "#trigger" ).on( "mouseout", function ( event ) {
var rt = event.relatedTarget;
if ( rt && $.contains( $menu[0], rt ) ) {
return; // still inside menu subtree
}
$menu.hide();
});
Move trigger → panel: menu stays open
Move trigger → outside: menu hides
How It Works
relatedTarget tells you where the pointer went. $.contains(menu, relatedTarget) detects movement into the dropdown rather than away from the whole widget.
📈 Practical Patterns
Direction logging, highlight effects, and target comparison.
Example 3 — mouseover vs mouseout: log relatedTarget direction
Bind both events on a zone — log whether relatedTarget represents entering or leaving relative to the zone.
jQuery
$( "#zone" ).on( "mouseover mouseout", function ( event ) {
var rt = event.relatedTarget;
var dir = event.type === "mouseover" ? "from" : "to";
var name = rt ? rt.nodeName : "(outside document)";
$( "#log" ).text( event.type + " — relatedTarget " + dir + ": " + name );
});
target: A (deepest element)
currentTarget: A (matched selector)
relatedTarget: DIV (entering element)
How It Works
target and currentTarget describe this event’s receiver; relatedTarget alone names the other element in the pointer move — essential for hover choreography.
Applications
🚀 Common Use Cases
Dropdown menus — avoid closing when relatedTarget is inside the menu panel.
Tooltip grace period — detect pointer moving from trigger into tooltip content.
Drag hover zones — know which drop target the pointer entered on mouseout.
Nested hover states — distinguish leaving a parent vs moving to a child.
Focus rings / previews — highlight the element the user is moving toward.
Debugging delegation — log transitions alongside target and delegateTarget.
🧠 How relatedTarget Works
1
Pointer crosses boundary
User moves from element A to element B — browser fires mouseout on A and mouseover on B.
Transition
2
Browser sets relatedTarget
On mouseout, relatedTarget is B (entering). On mouseover, it is A (leaving).
Native
3
jQuery handler
Your .on() callback reads event.relatedTarget as an Element (or null).
Read
4
✓
Smart hover logic
Keep menus open, flash destinations, or ignore internal child moves — using the other element in the transition.
Important
📝 Notes
Available since jQuery 1.1.4 — returns an Element (DOM node).
Read-only property on mouseover and mouseout.
On mouseout, points to the element being entered; on mouseover, the element being left.
May be null when the pointer leaves the document — guard before use.
mouseenter / mouseleave do not expose relatedTarget the same way — use mouseover/mouseout or $.contains().
Not the same as target — relatedTarget is always the other element in the move.
Compatibility
Browser Support
event.relatedTarget is forwarded on jQuery’s event object since jQuery 1.1.4+ for mouseover and mouseout. Underlying values come from the DOM MouseEvent relatedTarget property — supported in all browsers jQuery targets.
✓ jQuery 1.1.4+ · all browsers
jQuery event.relatedTarget
The other element in a pointer transition.
100%Universal
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
relatedTargetElement (DOM)
Bottom line: Read event.relatedTarget on mouseout — element the pointer is entering.
Wrap Up
Conclusion
event.relatedTarget names the other DOM element in a pointer transition — the official demo alerts its nodeName when leaving an anchor for a sibling DIV.
Use it for dropdown menus, hover grace periods, and transition highlights — and always distinguish it from target, currentTarget, and delegateTarget.
Use $.contains(container, relatedTarget) for menu panels
Wrap with $(event.relatedTarget).closest(selector) for nested targets
Prefer mouseover/mouseout when you need relatedTarget
Log alongside target/currentTarget when debugging hover bugs
❌ Don’t
Call event.relatedTarget() with parentheses
Assume relatedTarget equals target on mouseout
Read nodeName without a null check
Expect relatedTarget on keyboard-only events
Assign to event.relatedTarget — it is read-only
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about relatedTarget
The other element in a pointer move.
5
Core concepts
RT01
relatedTarget
Element (DOM)
API
out02
mouseout
Entering element
Event
demo03
Official
nodeName alert
Demo
tgt04
target
Event receiver
Compare
null05
Guard
Check before use
Safety
❓ Frequently Asked Questions
event.relatedTarget is an Element property on the jQuery event object. It returns the other DOM element involved in the event — the element the pointer is entering on mouseout, or leaving on mouseover. Available since jQuery 1.1.4 on mouseover and mouseout (and their delegated variants).
event.target is the element that received the event (often the deepest child under the pointer). event.relatedTarget is the other element in the pointer transition — where the mouse came from or is going to. On mouseout from a link to a DIV, target is the link and relatedTarget is the DIV.
The official API example binds mouseout on anchor tags and alerts event.relatedTarget.nodeName — typically "DIV" when the pointer moves from the link to a sibling block element.
mouseenter and mouseleave do not bubble and do not expose a meaningful relatedTarget in all browsers the same way mouseover/mouseout do. For hover menus that need to inspect the adjacent element, mouseover/mouseout (or checking relatedTarget with contains()) is the common pattern.
Yes. When the pointer leaves the document (moves outside the browser window) or enters from outside, relatedTarget may be null. Always guard with if (event.relatedTarget) before reading nodeName or calling jQuery methods.
On mouseout of a trigger, check whether event.relatedTarget is still inside the menu container (using $.contains or .closest). If the pointer moved into the dropdown panel rather than away entirely, keep the menu open instead of closing immediately.
Did you know?
The official jQuery event.relatedTarget demo alerts nodeName on mouseout from an anchor — when the pointer moves to a sibling DIV, the alert shows DIV, not A, because relatedTarget is the element being entered, not the one left behind.