jQuery event.relatedTarget

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Event object · jQuery 1.1.4+

What You’ll Learn

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

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.

Understanding event.relatedTarget

This property answers one question: which other element is involved in this pointer transition?

  • mouseoutrelatedTarget is the element the pointer is entering.
  • mouseoverrelatedTarget 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.

📝 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

  • NonerelatedTarget 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"
});

⚡ Quick Reference

EventrelatedTarget meansCommon use
mouseoutElement pointer is enteringKeep menu open if moving into panel
mouseoverElement pointer is leavingDetect where hover came from
mouseenterNot reliably exposedPrefer mouseover for relatedTarget
mouseleaveNot reliably exposedUse contains() on mouseout instead
nullPointer left documentAlways guard before access

📋 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)

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 );
  }
});
Try It Yourself

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();
});
Try It Yourself

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 );
});
Try It Yourself

How It Works

On mouseover, relatedTarget is where the pointer came from; on mouseout, where it is going — opposite sides of the same transition.

Example 4 — Highlight related element briefly on mouseout

When leaving a card, flash the destination element if relatedTarget is another card in the grid.

jQuery
$( ".card" ).on( "mouseout", function ( event ) {
  var rt = event.relatedTarget;
  if ( !rt ) return;
  var $related = $( rt ).closest( ".card" );
  if ( $related.length ) {
    $related.addClass( "flash" );
    setTimeout( function () { $related.removeClass( "flash" ); }, 400 );
  }
});
Try It Yourself

How It Works

Wrap relatedTarget with $(rt).closest(".card") to find the destination card even when the pointer hits a child node inside it.

Example 5 — Compare relatedTarget vs target vs currentTarget

Log all three element references on mouseout from a nested link inside a panel.

jQuery
$( "#panel" ).on( "mouseout", "a", function ( event ) {
  var rt = event.relatedTarget;
  $( "#log" ).html(
    "target: " + event.target.nodeName + "
" + "currentTarget: " + event.currentTarget.nodeName + "
" + "relatedTarget: " + ( rt ? rt.nodeName : "null" ) ); });
Try It Yourself

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.

🚀 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.

📝 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
relatedTarget Element (DOM)

Bottom line: Read event.relatedTarget on mouseout — element the pointer is entering.

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.

💡 Best Practices

✅ Do

  • Guard: if (event.relatedTarget) { … }
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about relatedTarget

The other element in a pointer move.

5
Core concepts
out 02

mouseout

Entering element

Event
demo 03

Official

nodeName alert

Demo
tgt 04

target

Event receiver

Compare
null 05

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.

Next: event.result

Read the last non-undefined return from earlier handlers in the chain.

event.result tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful