JavaScript MouseEvent relatedTarget Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

MouseEvent.relatedTarget is the secondary EventTarget for enter/leave style mouse events—the other element the pointer came from or went to. Learn MDN’s target vs relatedTarget table, when the value is null, and five try-it labs.

01

Kind

Instance property

02

Returns

EventTarget | null

03

Status

Baseline Widely available

04

Used on

over / out / enter / leave

05

Also

dragenter / dragleave

06

Safe read

Null-check first

Introduction

When the pointer slides from a red box into a blue box, event.target tells you one side of the story. relatedTarget tells you the other side—where you came from on mouseover, or where you went on mouseout.

JavaScript
el.addEventListener("mouseover", (event) => {
  console.log(event.target);         // element entered
  console.log(event.relatedTarget);  // element exited (or null)
});
💡
Beginner tip

Always write event.relatedTarget ? event.relatedTarget.id : "unknown" (as MDN does). A missing secondary target is null, and reading .id on null throws.

Understanding the Property

MDN: MouseEvent.relatedTarget is the secondary target for the mouse event, if there is one. For events with no secondary target, it returns null. FocusEvent.relatedTarget is a similar property for focus events.

  • Instance — read event.relatedTarget in a handler.
  • EventTarget or null — often an Element; sometimes missing.
  • Secondary — complements event.target on transition events.
  • Read-only — may be set via constructor options on synthetic events.

📝 Syntax

JavaScript
const other = mouseEvent.relatedTarget;

Value

An EventTarget object or null (MDN).

📚 target vs relatedTarget

MDN’s mapping for the common transition events:

EventtargetrelatedTarget
mouseenterElement enteredElement exited
mouseleaveElement exitedElement entered
mouseoutElement exitedElement entered
mouseoverElement enteredElement exited
dragenterElement enteredElement exited
dragleaveElement exitedElement entered

⚡ Quick Reference

GoalCode
Read secondary targetevent.relatedTarget
Safe idevent.relatedTarget ? event.relatedTarget.id : "unknown"
Primary elementevent.target
Typical eventsmouseover / mouseout / enter / leave
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

Four facts about relatedTarget.

Kind
instance

On the event

Type
EventTarget?

or null

Role
secondary

Other element

Status
baseline

Widely available

Examples Gallery

Examples follow MDN MouseEvent.relatedTarget. Move between colored boxes in the try-it labs to see both targets.

📚 Getting Started

Watch relatedTarget while crossing element boundaries.

Example 1 — Red / Blue Boxes (MDN Pattern)

Log where the pointer came from and went on mouseover / mouseout.

JavaScript
const log = document.getElementById("log");
const red = document.getElementById("red");
const blue = document.getElementById("blue");

function outListener(event) {
  const related = event.relatedTarget ? event.relatedTarget.id : "unknown";
  log.innerText = `\nfrom ${event.target.id} into ${related} ${log.innerText}`;
}

function overListener(event) {
  const related = event.relatedTarget ? event.relatedTarget.id : "unknown";
  log.innerText = `\ninto ${event.target.id} from ${related} ${log.innerText}`;
}

red.addEventListener("mouseover", overListener);
red.addEventListener("mouseout", outListener);
blue.addEventListener("mouseover", overListener);
blue.addEventListener("mouseout", outListener);
Try It Yourself

How It Works

On mouseover, target is the element entered and relatedTarget is the one exited. On mouseout those roles swap.

Example 2 — mouseenter / mouseleave

Same secondary-target idea, without bubbling through children the same way.

JavaScript
panel.addEventListener("mouseenter", (e) => {
  console.log("enter", e.target.id, "from", e.relatedTarget && e.relatedTarget.id);
});

panel.addEventListener("mouseleave", (e) => {
  console.log("leave", e.target.id, "to", e.relatedTarget && e.relatedTarget.id);
});
Try It Yourself

How It Works

Per MDN: on mouseenter, target is entered and relatedTarget is exited; on mouseleave it is the reverse.

📈 Null Cases, Safety & Synthetic Events

Handle missing targets and construct events in tests.

Example 3 — relatedTarget Is Often null on Click

Events without a secondary target do not invent one.

JavaScript
button.addEventListener("click", (e) => {
  console.log(e.relatedTarget); // typically null
  console.log(e.target);        // the button
});
Try It Yourself

How It Works

MDN: for events with no secondary target, relatedTarget returns null. Clicks are a common case.

Example 4 — Safe Helper for the Secondary Id

Reuse MDN’s null-safe pattern in one helper.

JavaScript
function relatedId(event) {
  return event.relatedTarget ? event.relatedTarget.id || "(no id)" : "unknown";
}

el.addEventListener("mouseout", (e) => {
  console.log(`left ${e.target.id} toward ${relatedId(e)}`);
});
Try It Yourself

How It Works

The helper avoids crashes when relatedTarget is missing or has no id.

Example 5 — Synthetic Event with relatedTarget

Useful in tests that assert enter/leave transitions.

JavaScript
const from = document.getElementById("a");
const to = document.getElementById("b");

const event = new MouseEvent("mouseover", {
  bubbles: true,
  relatedTarget: from
});

to.dispatchEvent(event);
console.log(event.relatedTarget === from); // true when supported
console.log(event.target === to);          // true after dispatch to `to`
Try It Yourself

How It Works

Pass relatedTarget in MouseEvent() options, then dispatch onto the primary target.

🚀 Common Use Cases

  • Logging which element the pointer came from or went to.
  • Hover menus that ignore moves into a related dropdown panel.
  • Highlighting “from → to” paths while teaching mouse events.
  • Drag-and-drop enter/leave debugging with dragenter / dragleave.
  • Unit tests that construct over/out events with a known secondary target.

🔧 How It Works

1

Pointer crosses a boundary

The cursor leaves one element and enters another.

Input
2

Browser fires a transition event

mouseover, mouseout, enter, leave, or drag variants.

Event
3

Both targets are filled

target is primary; relatedTarget is the other side (or null).

Targets
4

Your handler branches

Null-check, then use ids or containment checks for UX.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Returns null when there is no secondary target.
  • Also used on dragenter / dragleave (MDN table).
  • Related: pageY, addEventListener(), MouseEvent(), Window, JavaScript hub.

Universal Browser Support

MouseEvent.relatedTarget is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MouseEvent.relatedTarget

Reliable secondary EventTarget for mouseover/out/enter/leave and drag enter/leave.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in modern IE versions
Full support
relatedTarget Excellent

Bottom line: Read event.relatedTarget on transition events; always null-check before using it.

Conclusion

event.relatedTarget is the secondary element in pointer enter/leave transitions. Pair it with event.target, null-check before reading properties, and use MDN’s event table to remember which role each event uses.

Continue with pageY, screenX, addEventListener(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Null-check relatedTarget before reading .id
  • Use it on over/out/enter/leave (and drag enter/leave)
  • Keep MDN’s target / relatedTarget table nearby
  • Prefer clear ids when debugging transitions
  • Test hover menus that care about the “other” element

❌ Don’t

  • Assume relatedTarget is set on every mouse event
  • Read properties on null without a guard
  • Confuse it with event.target
  • Forget that roles swap between over and out
  • Mutate event.relatedTarget on live user events

Key Takeaways

Knowledge Unlocked

Five things to remember about relatedTarget

Secondary EventTarget for mouse transition events.

5
Core concepts
🎯 02

Secondary

other element

Role
🔢 03

Nullable

EventTarget | null

Type
📈 04

Transitions

over / out / enter

Events
🛡️ 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It is a read-only secondary EventTarget for certain mouse (and drag) events—the other element involved when the pointer moves between nodes. If there is no secondary target, it is null.
No. MDN marks MouseEvent.relatedTarget as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed.
MDN lists mouseenter, mouseleave, mouseout, mouseover, dragenter, and dragleave. For each, target and relatedTarget describe which element you entered and which you exited (roles swap by event type).
For events with no secondary target—or when the other side is outside the document—relatedTarget is null. Always null-check before reading .id or other properties.
event.target is the primary element for the event. relatedTarget is the other element in enter/leave/over/out transitions. On a plain click, relatedTarget is typically null.
Yes. FocusEvent.relatedTarget is a similar secondary target for focus and blur events.
Did you know?

Focus events have their own twin: FocusEvent.relatedTarget. The idea is the same—a secondary target that describes the other end of a focus move—just for keyboard/focus instead of the mouse.

Next: MouseEvent.screenX

Learn horizontal mouse position in screen (monitor) coordinates.

screenX →

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.

5 people found this page helpful