Element: setPointerCapture() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2020.

The setPointerCapture() method of the Element interface is used to designate a specific element as the capture target of future pointer events. Subsequent events for the pointer will be targeted at the capture element until capture is released (via Element.releasePointerCapture() or the pointerup event is fired).

See pointer events for an overview and examples of how pointer capture works.

Syntax

js
setPointerCapture(pointerId)

Parameters

pointerId

The pointerId of a PointerEvent object.

Return value

None (undefined).

Exceptions

NotFoundError DOMException

Thrown if pointerId does not match any active pointer.

Examples

This example sets pointer capture on a <div> when you press down on it. This lets you slide the element horizontally, even when your pointer moves outside of its boundaries.

HTML

html
<div id="slider">SLIDE ME</div>

CSS

css
div {
  width: 140px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #ffbbee;
  touch-action: none;
}

JavaScript

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

function beginSliding(e) {
  slider.onpointermove = slide;
  slider.setPointerCapture(e.pointerId);
}

function stopSliding(e) {
  slider.onpointermove = null;
  slider.releasePointerCapture(e.pointerId);
}

function slide(e) {
  slider.style.transform = `translate(${e.clientX - 70}px)`;
}

slider.onpointerdown = beginSliding;
slider.onpointerup = stopSliding;

Result

Specifications

Specification
Pointer Events
# dom-element-setpointercapture

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
setPointerCapture
Chrome – Full support
Chrome 55 (Release date: 2016-12-01)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 59 (Release date: 2018-03-13)
footnote
footnote Before Firefox 82, setPointerCapture() throws InvalidPointerId for an invalid pointerId argument. From Firefox 82, it throws the specified NotFoundError exception. See bug 1662124.
Opera – Full support
Opera 42 (Release date: 2016-12-13)
footnote Full support
Safari – Full support
Safari 13 (Release date: 2019-09-19)
footnote Full support
Chrome Android – Full support
Chrome Android 55 (Release date: 2016-12-06)
footnote Full support
Firefox for Android – Full support
Firefox for Android 79 (Release date: 2020-07-28)
footnote
footnote Before Firefox 82, setPointerCapture() throws InvalidPointerId for an invalid pointerId argument. From Firefox 82, it throws the specified NotFoundError exception. See bug 1662124.
Opera Android – Full support
Opera Android 42 (Release date: 2017-01-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 13 (Release date: 2019-09-19)
footnote Full support
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – Full support
WebView Android 55 (Release date: 2016-12-06)
footnote Full support
WebView on iOS – Full support
WebView on iOS 13 (Release date: 2019-09-19)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also