JavaScript MouseEvent relatedTarget Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

MouseEvent.relatedTarget is a read-only secondary EventTarget for mouse (and drag) transitions — the other element involved when the pointer moves between nodes. If there is no secondary target, it is null.

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use event.relatedTarget. Start with this line.

JavaScript
event.relatedTarget
CallWhat it does
event.relatedTargetPrimary signature.

Built into the browser. Read event.relatedTarget on a MouseEvent — no import required.

Library declarations
JavaScript
event.relatedTarget

Parameters

ParameterDescription
Enter / leave / over / out eventRequired. Events that fill relatedTarget

Return Value

Return ValueDescription
*The current value of event.relatedTarget.

JavaScript Examples

Example 1 — Log mouseover / mouseout

MDN-style log: which element you entered and which you left.

JavaScript
function label(node) {
  return node ? (node.id || node.tagName) : "null";
}

function onOver(e) {
  console.log("into", label(e.target), "from", label(e.relatedTarget));
}

function onOut(e) {
  console.log("from", label(e.target), "into", label(e.relatedTarget));
}

red.addEventListener("mouseover", onOver);
red.addEventListener("mouseout", onOut);
blue.addEventListener("mouseover", onOver);
blue.addEventListener("mouseout", onOut);
Try It Yourself

How It Works

  1. Label safely. If relatedTarget is null, print "null".
  2. On over. target is entered; relatedTarget is exited.
  3. On out. Roles swap — target is exited; relatedTarget is entered.
  4. Move between nodes to see both sides of the transition.

Example 2 — mouseenter / mouseleave

Same secondary target idea, but these events do not bubble (MDN).

JavaScript
box.addEventListener("mouseenter", (e) => {
  console.log({
    type: e.type,
    target: e.target.id,
    related: e.relatedTarget && e.relatedTarget.id
  });
});

box.addEventListener("mouseleave", (e) => {
  console.log({
    type: e.type,
    target: e.target.id,
    related: e.relatedTarget && e.relatedTarget.id
  });
});
Try It Yourself

How It Works

  1. Listen on one box. Enter and leave fire when the pointer crosses that element’s boundary.
  2. Read both sides. Still use target + relatedTarget.
  3. Guard with &&. Skip .id when relatedTarget is null.
  4. No bubbling. Unlike mouseover/mouseout, these stay on the element you attached.

Example 3 — Null-Safe Label Helper

Always treat relatedTarget as optional before reading properties.

JavaScript
function label(node) {
  if (!node) return "null";
  return node.id || node.nodeName;
}

document.addEventListener("click", (e) => {
  console.log({
    target: label(e.target),
    relatedTarget: label(e.relatedTarget) // usually "null" on click
  });
});
Try It Yourself

How It Works

  1. Write a helper. One function handles both nodes and null.
  2. Click has no secondary target. MDN: events without a secondary target return null.
  3. Still log both. Compare with over/out logs where relatedTarget is filled.
  4. Avoid crashes. Never assume e.relatedTarget.id exists.

Browser Support

MouseEvent.relatedTarget is Baseline Widely available (MDN: since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline · Widely available

MouseEvent.relatedTarget

Secondary EventTarget for mouse and drag enter/leave transitions — null when there is no other element.

Universal Widely available
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 Excellent

Bottom line: Safe to use in modern browsers. Pair with event.target; null-check before reading properties. See FocusEvent.relatedTarget for focus/blur.

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

Next: MouseEvent.screenX

Learn the horizontal pointer position relative to the whole screen.

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

11 people found this page helpful