Document: pointerLockElement property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

The pointerLockElement read-only property of the Document interface provides the element set as the target for mouse events while the pointer is locked. It is null if lock is pending, pointer is unlocked, or the target is in another document.

Value

An Element or null.

Examples

Checking pointer lock status

This example contains a <div> element that in turn contains a <button>. Clicking the button requests pointer lock for the <div>.

The example also listens for the pointerlockchange event: when this event is fired, the event handler disables the "Lock" button if an element in the document has the pointer lock, and enables the button otherwise.

The effect of this is that if you click the "Lock" button, the pointer is locked and the button is disabled: if you then exit pointer lock (for example, by pressing the Escape key), the button is enabled again.

HTML

html
<div id="container">
  <button id="lock">Lock</button>
</div>

CSS

css
div {
  height: 100px;
  width: 200px;
  border: 2px solid blue;
}

JavaScript

js
const lock = document.querySelector("#lock");
const container = document.querySelector("#container");

lock.addEventListener("click", () => {
  container.requestPointerLock();
});

document.addEventListener("pointerlockchange", () => {
  const locked = document.pointerLockElement;
  lock.disabled = Boolean(locked);
});

Result

Specifications

Specification
Pointer Lock 2.0
# dom-documentorshadowroot-pointerlockelement

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
pointerLockElement
Chrome – Full support
Chrome 37 (Release date: 2014-08-26)
footnote Full support
Edge – Full support
Edge 13 (Release date: 2015-11-12)
footnote Full support
Firefox – No support
Firefox 14 – 49 (Release date: 2012-07-17)
prefix
prefix Implemented with the vendor prefix: moz
Firefox – Full support
Firefox 50 (Release date: 2016-11-15)
footnote Full support
Opera – Full support
Opera 24 (Release date: 2014-09-02)
footnote Full support
Safari – Full support
Safari 10.1 (Release date: 2017-03-27)
footnote Full support
Chrome Android – No support
Chrome Android
footnote
footnote See bug 40290045
Firefox for Android – No support
Firefox for Android 14 – 49 (Release date: 2012-06-26)
prefix
prefix Implemented with the vendor prefix: moz
Firefox for Android – Full support
Firefox for Android 50 (Release date: 2016-11-15)
footnote Full support
Opera Android – No support
Opera Android
footnote
footnote See bug 40290045
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – No support
Samsung Internet
footnote
footnote See bug 40290045
WebView Android – No support
WebView Android
footnote
footnote See bug 40290045
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

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

Full support
Full support
No support
No support
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.

See also