JavaScript Element setCapture() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Non-standard
Instance method

What You’ll Learn

Element.setCapture() is a deprecated, non-standard instance method that retargets mouse events to an element after mousedown. Learn MDN’s coordinate-tracking demo, the retargetToElement flag, document.releaseCapture() cleanup, feature detection, and why setPointerCapture() replaced it—plus five try-it labs aligned with MDN.

01

Kind

Instance method

02

Trigger

On mousedown

03

Returns

undefined

04

Status

Deprecated · Non-standard

05

Modern alt

setPointerCapture

06

Release

mouseup / releaseCapture

Introduction

When a user presses the mouse button on an element and drags, the pointer can leave that element’s bounds. Normally, mousemove events would stop targeting the original element. Legacy Gecko code used setCapture() to keep receiving mouse events until the button was released.

Call element.setCapture(retargetToElement) during a mousedown handler (MDN). Capture ends when the user releases the mouse button or when document.releaseCapture() is called.

💡
Modern replacement (MDN)

MDN warns this API never had much cross-browser support. For new code, use element.setPointerCapture(pointerId) from the Pointer Events API instead. It works across modern browsers for mouse, pen, and touch.

This page is part of JavaScript Element. Pair with MouseEvent, EventTarget, and scrollIntoViewIfNeeded() (another non-standard Element API).

Understanding the setCapture() Method

Calling el.setCapture() during mousedown redirects subsequent mouse events to el until capture is released. It was introduced for Gecko 2.0 mouse-capture scenarios (MDN).

  • It is an instance method on Element.
  • retargetToElement — if true, all events target this element; if false, descendants may also receive events (MDN).
  • Returns undefined.
  • Deprecated and non-standard on MDN — not part of any specification.
  • Ends on mouse button release or document.releaseCapture() (MDN).
  • Modern alternative: setPointerCapture() / releasePointerCapture().

📝 Syntax

General form of Element.setCapture (MDN):

JavaScript
setCapture(retargetToElement)

Parameters

ParameterTypeDescription
retargetToElementbooleanIf true, all events target this element; if false, descendants may also receive events (MDN).

Return value

TypeDescription
undefinedNo return value (MDN).

Notes

  • Call during mousedown handling (MDN).
  • Capture ends on mouse button release or document.releaseCapture() (MDN).
  • The element may not scroll fully to top/bottom depending on layout (MDN).

Common patterns

JavaScript
const btn = document.getElementById("myButton");

function onMouseDown(e) {
  if (!e.target.setCapture) return;
  e.target.setCapture();
  e.target.addEventListener("mousemove", onMouseMove);
}

function onMouseUp(e) {
  e.target.removeEventListener("mousemove", onMouseMove);
}

function onMouseMove(e) {
  console.log(e.clientX, e.clientY);
}

if (btn.setCapture) {
  btn.addEventListener("mousedown", onMouseDown);
  btn.addEventListener("mouseup", onMouseUp);
}

⚡ Quick Reference

GoalCode
Start mouse captureel.setCapture() on mousedown
Retarget only to elementel.setCapture(true)
Allow descendant targetsel.setCapture(false)
Release programmaticallydocument.releaseCapture()
Feature detectif (el.setCapture) { ... }
MDN statusDeprecated · Non-standard

🔍 At a Glance

Four facts to remember about Element.setCapture().

Returns
undefined

No value

Status
deprecated

Non-standard

When
mousedown

Start capture

Modern
setPointerCapture

Use instead

📋 setCapture() vs setPointerCapture()

setCapture(retarget)setPointerCapture(id)addEventListener on document
StatusDeprecated, non-standardStandard Pointer EventsStandard DOM Events
Input typesMouse only (legacy)Mouse, pen, touchAny event type
TriggerOn mousedown (MDN)On pointerdownManual wiring
Releasemouseup / releaseCapturepointerup / releasePointerCaptureremoveEventListener
Best forLegacy Gecko maintenanceNew drag/slider UIsFallback patterns

Examples Gallery

Examples follow MDN Element.setCapture() patterns. Use View Output or Try It Yourself for each case. Note: this API may be unavailable in your browser—examples include feature detection.

📚 Getting Started

MDN’s mouse-coordinate demo and the retargetToElement flag.

Example 1 — Track Mouse Coordinates (MDN)

On mousedown, call setCapture() and draw coordinates while the button is held.

JavaScript
function mouseDown(e) {
  e.target.setCapture();
  e.target.addEventListener("mousemove", mouseMoved);
}

function mouseUp(e) {
  e.target.removeEventListener("mousemove", mouseMoved);
}

function mouseMoved(e) {
  const output = document.getElementById("output");
  output.textContent = `Position: ${e.clientX}, ${e.clientY}`;
}

const btn = document.getElementById("myButton");
if (btn.setCapture) {
  btn.addEventListener("mousedown", mouseDown);
  btn.addEventListener("mouseup", mouseUp);
}
Try It Yourself

How It Works

MDN: call setCapture() during mousedown so mousemove keeps firing on the element even when the pointer leaves it. Remove the listener on mouseup.

Example 2 — retargetToElement Flag

Pass true to target only this element; false allows descendant targets (MDN).

JavaScript
const panel = document.getElementById("panel");

panel.addEventListener("mousedown", (e) => {
  if (!panel.setCapture) return;
  // true — all events go to panel only (MDN)
  panel.setCapture(true);
});

panel.addEventListener("mouseup", () => {
  // capture ends automatically on button release (MDN)
});
Try It Yourself

How It Works

MDN: when retargetToElement is false, events can still fire on descendants. When true, they are directed only to the capturing element.

📈 Practical Patterns

Feature detection, cleanup, and modern migration.

Example 3 — Feature Detection (MDN)

Guard calls with if (el.setCapture) because support is not universal.

JavaScript
const btn = document.getElementById("myButton");
const output = document.getElementById("output");

if (btn.setCapture) {
  btn.addEventListener("mousedown", (e) => e.target.setCapture());
  output.textContent = "setCapture supported";
} else {
  output.textContent =
    "Sorry, no setCapture support in this browser";
}
Try It Yourself

How It Works

MDN’s live example uses the same guard. Most Chromium and WebKit browsers lack setCapture(), so always detect before calling.

Example 4 — Cleanup with releaseCapture()

End capture early with document.releaseCapture() (MDN).

JavaScript
const handle = document.getElementById("handle");

handle.addEventListener("mousedown", (e) => {
  if (!handle.setCapture) return;
  handle.setCapture();
  handle.addEventListener("mousemove", onMove);
});

function onMove(e) {
  if (e.clientY < 10 && document.releaseCapture) {
    document.releaseCapture();
    handle.removeEventListener("mousemove", onMove);
  }
}
Try It Yourself

How It Works

MDN: capture normally ends when the mouse button is released. Call document.releaseCapture() to stop capture programmatically and remove listeners.

Example 5 — Modern Alternative: setPointerCapture()

Migrate legacy capture code to the standard Pointer Events API (MDN recommendation).

JavaScript
const slider = document.getElementById("slider");

slider.addEventListener("pointerdown", (e) => {
  slider.setPointerCapture(e.pointerId);
});

slider.addEventListener("pointermove", (e) => {
  if (slider.hasPointerCapture(e.pointerId)) {
    console.log(e.clientX, e.clientY);
  }
});

slider.addEventListener("pointerup", (e) => {
  slider.releasePointerCapture(e.pointerId);
});
Try It Yourself

How It Works

MDN recommends setPointerCapture() for new drag and slider UIs. It works across modern browsers for mouse, pen, and touch input.

🚀 Common Use Cases

  • Legacy drag handles that must keep receiving mousemove outside the element (Gecko-only).
  • Custom slider or drawing tools in old Firefox/XUL-era codebases.
  • Understanding why modern apps use setPointerCapture() instead.
  • Migrating maintenance code that still calls setCapture() on mousedown.
  • Pairing with document.releaseCapture() for early cancellation.
  • Teaching event retargeting concepts before learning Pointer Events.

🧠 How setCapture() Retargets Mouse Events

1

User presses mouse on element

In your mousedown handler, call el.setCapture() (MDN).

mousedown
2

Events retarget to captor

Mouse events route to the capturing element until release (MDN).

Capture
3

Track outside bounds

mousemove keeps firing even when the pointer leaves the element.

Drag
4

Capture ends on mouseup

Release the button or call document.releaseCapture(). Return value is undefined.

📝 Notes

  • Deprecated and non-standard on MDN — not part of any specification.
  • MDN: never had much cross-browser support; avoid in new production code.
  • Modern replacement: element.setPointerCapture(pointerId) (Pointer Events).
  • Call during mousedown; ends on mouse button release or document.releaseCapture() (MDN).
  • Element may not scroll fully to top/bottom depending on layout (MDN).
  • Related: MouseEvent, EventTarget, setAttributeNS(), JavaScript hub.

Limited / Legacy Browser Support

Element.setCapture() is deprecated and non-standard (MDN). It was a legacy Gecko mouse-capture API with little cross-browser support. Logos use the shared browser-image-sprite.png sprite from this project.

Deprecated · Non-standard

Element.setCapture()

Legacy Gecko mouse capture — use setPointerCapture() for new drag and pointer-tracking UIs.

Legacy Not for new apps
Google Chrome Not supported
No
Microsoft Edge Not supported
No
Mozilla Firefox Legacy Gecko API; may be absent in modern builds
Legacy
Apple Safari Not supported
No
Opera Not supported
No
Internet Explorer Not supported
No
setCapture() Deprecated

Bottom line: Feature-detect with if (el.setCapture) in legacy code only. For new projects, use setPointerCapture() from the Pointer Events API.

Conclusion

Element.setCapture() was a legacy way to keep receiving mouse events after mousedown. MDN marks it deprecated and non-standard, with limited browser support. Returns undefined.

For new code, use setPointerCapture(). Continue with setAttributeNS(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use setPointerCapture() for new drag/slider UIs
  • Feature-detect with if (el.setCapture) in legacy code
  • Remove mousemove listeners on mouseup
  • Call document.releaseCapture() when canceling early
  • Document Gecko-only behavior for maintainers

❌ Don’t

  • Use setCapture() in new cross-browser projects
  • Assume capture works in Chrome, Safari, or Edge
  • Forget to clean up listeners after capture ends
  • Expect a useful return value (it is undefined)
  • Confuse with setPointerCapture() or event bubbling

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.setCapture()

Legacy mouse capture — deprecated, use Pointer Events instead.

5
Core concepts
📄 02

Trigger

mousedown

Event
⚠️ 03

Status

deprecated

Legacy
🖱️ 04

Modern

setPointerCapture

Prefer
05

Release

mouseup

End

❓ Frequently Asked Questions

During mousedown handling, it retargets all mouse events to the element until the mouse button is released or document.releaseCapture() is called (MDN).
MDN marks Element.setCapture() as Deprecated and Non-standard. It is not Experimental, but it is not part of any specification and should not be used in new production code.
undefined. There is no return value (MDN).
If true, all events target this element directly. If false, events can also fire on descendants of this element (MDN).
MDN recommends element.setPointerCapture() from the Pointer Events API for modern drag-and-track-pointer workflows.
No practical cross-browser support. MDN notes it never had much cross-browser support and was primarily a legacy Gecko API.
Did you know?

MDN introduced setCapture() for Gecko 2.0 mouse capture, but the web platform moved on to setPointerCapture()—which handles mouse, pen, and touch with standard cross-browser support.

Next: setHTML()

Learn XSS-safe HTML injection with the Sanitizer API.

setHTML() →

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.

8 people found this page helpful