JavaScript Document pointerLockElement Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Limited availability
Instance property

What You’ll Learn

Document.pointerLockElement is a read-only instance property that returns the Element receiving locked mouse events, or null. Learn MDN’s pointerlockchange pattern, how it parallels fullscreenElement, FPS game workflows, and five examples with try-it labs.

01

Kind

Read-only property

02

Returns

Element | null

03

Active?

!== null

04

Exit

exitPointerLock

05

API

Pointer Lock

06

Status

Limited avail.

Introduction

Pointer lock lets web apps capture the mouse for first-person games, 3D viewers, and creative tools. While the pointer is locked, the browser hides the cursor and sends continuous movementX / movementY values. Scripts need to know which element owns that lock—that is what document.pointerLockElement provides.

MDN: the read-only property returns the Element set as the target for mouse events while the pointer is locked, or null if the lock is pending, the pointer is unlocked, or the target is in another document.

💡
Parallel to fullscreen and PiP

Think of it like document.fullscreenElement or document.pictureInPictureElement: one property tells you whether a special mode is on and which DOM element is involved.

Related Document tutorials: fullscreenElement, activeElement, requestPointerLock(), Document constructor.

Understanding Document.pointerLockElement

A read-only instance property on Document. Its value is the element with an active pointer lock, or null when no lock applies to this document.

  • ValueElement reference with lock, or null (MDN).
  • Read-only — assigning does not throw; the setter is ignored (MDN no-op).
  • Null cases — unlocked, lock pending, or locked element in another document (MDN).
  • Typical element — often a <canvas> or game container after requestPointerLock().
  • Exit — pair with document.exitPointerLock() when truthy, or Esc (MDN).

📝 Syntax

JavaScript
document.pointerLockElement

Value

An Element object — or null when pointer lock is not active for this document (MDN).

MDN example — sync Lock button on pointerlockchange

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

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

document.addEventListener("pointerlockchange", () => {
  if (document.pointerLockElement === container) {
    lockButton.disabled = true;
  } else {
    lockButton.disabled = false;
  }
});

Check if pointer lock is active

JavaScript
const isLocked = document.pointerLockElement !== null;
console.log("Pointer locked?", isLocked);

⚡ Quick Reference

GoalCode / note
Locked element or nulldocument.pointerLockElement
Is lock active?document.pointerLockElement !== null
Exit lockdocument.exitPointerLock()
Enter lockelement.requestPointerLock() from user gesture
Sync UIpointerlockchange / pointerlockerror events
MDN statusLimited availability (not Baseline)

🔍 At a Glance

Four facts about document.pointerLockElement.

Type
Element|null

Read-only

Mode
Pointer lock

Mouse capture

Active?
!== null

Status check

Status
limited

Not Baseline

📋 pointerLockElement vs fullscreenElement

pointerLockElementfullscreenElement
PurposeHide cursor; continuous mouse movementFill screen with an element
Typical useFPS games, 3D viewersVideo, games, presentations
Enter APIelement.requestPointerLock()element.requestFullscreen()
Exit APIdocument.exitPointerLock() or Escdocument.exitFullscreen() or Esc

Examples Gallery

Examples follow MDN Document: pointerLockElement. Try-it labs read the property safely; request pointer lock from a user click where the browser supports it.

📚 Getting Started

Read the property and detect pointer lock status.

Example 1 — Read When Pointer Lock Is Off

On a normal page, the property is null until lock starts.

JavaScript
console.log(document.pointerLockElement);
// null when pointer is not locked
console.log(document.pointerLockElement === null);
Try It Yourself

How It Works

null means no element from this document currently has the pointer locked.

Example 2 — MDN pointerlockchange Button Sync

Disable the Lock button while pointerLockElement matches the container (MDN example).

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

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

document.addEventListener("pointerlockchange", () => {
  lockButton.disabled = document.pointerLockElement === container;
});
Try It Yourself

How It Works

pointerlockchange fires whenever lock starts, ends, or moves to another element.

📈 Games & Events

Guard exit calls and keep UI in sync with lock state.

Example 3 — Inspect the Locked Element

When lock is on, read nodeName or id from the returned element.

JavaScript
const el = document.pointerLockElement;
if (el) {
  console.log(el.nodeName); // often "CANVAS" or "DIV"
  console.log(el.id);
} else {
  console.log("No locked element");
}
Try It Yourself

How It Works

The same element reference you passed to requestPointerLock() is returned while lock is active.

Example 4 — Exit Only When Locked

Call exitPointerLock() only when the property is truthy.

JavaScript
function exitPointerLockSafe() {
  if (document.pointerLockElement) {
    document.exitPointerLock();
  }
}

exitPointerLockSafe(); // no-op when null
Try It Yourself

How It Works

Guarding on pointerLockElement avoids redundant exit calls when lock is already off.

Example 5 — Listen for pointerlockchange

Update status text when pointerLockElement changes.

JavaScript
function updateLockUI() {
  const active = document.pointerLockElement !== null;
  console.log("Pointer locked?", active);
}

document.addEventListener("pointerlockchange", updateLockUI);
document.addEventListener("pointerlockerror", () => {
  console.log("Pointer lock request failed");
});
Try It Yourself

How It Works

Events fire on the document when lock starts or ends—use them instead of polling the property.

🚀 Common Use Cases

  • FPS camera control — read movementX / movementY only when pointerLockElement matches your canvas.
  • Toggle Lock button — disable while locked (MDN pattern).
  • Custom unlock control — call exitPointerLock() when truthy.
  • Game pause menu — exit lock when opening UI overlays.
  • Debugging — log which element owns lock during development.
  • Cross-mode checks — combine with fullscreenElement for immersive apps.

🧠 How Pointer Lock Status Works

1

User requests lock

Click a game area or call element.requestPointerLock() from a gesture.

Enter
2

Property updates

document.pointerLockElement references the locked element.

Set
3

Mouse events route to element

Cursor hidden; pointermove delivers relative movement values.

Play
4

Exit clears to null

exitPointerLock(), Esc, or tab away resets the property to null.

📝 Notes

  • MDN: Limited availability (not Baseline) — no Deprecated / Experimental / Non-standard banner.
  • Assigning to the property is a no-op; it never throws, even in strict mode (MDN).
  • Returns null while a lock request is pending or when the locked element is in another document (MDN).
  • Enter lock only from a user gesture; browsers block unsolicited lock requests.
  • Related: requestPointerLock(), fullscreenElement, Document constructor.

Browser Support

Document.pointerLockElement is marked Limited availability on MDN (not Baseline). Feature-detect before production use. Logos use the shared browser-image-sprite.png sprite from this project.

Limited availability · Not Baseline

Document.pointerLockElement

Read-only Element with pointer lock, or null — Pointer Lock API status check.

Limited Check compat
Google Chrome Supported · Desktop
Supported
Mozilla Firefox Supported in modern versions
Supported
Apple Safari Supported on desktop Safari
Supported
Microsoft Edge Chromium Pointer Lock support
Supported
Opera Follow Chromium behavior
Supported
Internet Explorer No Pointer Lock API
Not supported
Document.pointerLockElement Limited availability

Bottom line: Use pointerLockElement !== null to detect lock. Pair with requestPointerLock / exitPointerLock and pointerlockchange / pointerlockerror events.

Conclusion

Document.pointerLockElement tells you which element (if any) currently owns pointer lock. Check it before calling exitPointerLock(), mirror the pattern of fullscreenElement, and keep UI in sync with pointerlockchange events.

Continue with prerendering, requestPointerLock(), fullscreenElement, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check document.pointerLockElement !== null
  • Compare to your target element: === container (MDN)
  • Start lock only from a user gesture (click / tap)
  • Listen for pointerlockchange and pointerlockerror
  • Guard exitPointerLock() with a truthy property check

❌ Don’t

  • Assign to pointerLockElement expecting to enter lock
  • Assume pointer lock works on every platform (Limited availability)
  • Expect parent-document access to iframe lock targets (MDN: null)
  • Auto-lock without user interaction
  • Poll the property in a tight loop instead of using events

Key Takeaways

Knowledge Unlocked

Five things to remember about pointerLockElement

Element or null — the pointer lock status property.

5
Core concepts
02

Active

!== null

Check
🚪03

Exit

exitPointerLock

MDN
🎯04

Enter

requestLock

Gesture
🔄05

Events

lockchange

Sync UI

❓ Frequently Asked Questions

The Element set as the target for mouse events while the pointer is locked in this document, or null if the lock is pending, the pointer is unlocked, or the target is in another document (MDN).
No. MDN marks it as Limited availability (not Baseline), but not Deprecated, Experimental, or Non-standard. Feature-detect before relying on pointer lock in production.
When pointer lock is not active, while a lock request is still pending, or when the locked element belongs to another document such as an iframe (MDN).
No meaningful assignment. The property is read-only; assigning does not throw even in strict mode — the setter is a no-operation and is ignored (MDN).
Call document.exitPointerLock(), or let the user press Esc. Listen for pointerlockchange and check document.pointerLockElement.
fullscreenElement returns the element in fullscreen mode. pointerLockElement returns the element receiving locked mouse events. Games often use both together.
Did you know?

MDN notes that although pointerLockElement is read-only, assigning to it does not throw—even in strict mode. The setter is ignored. To enter pointer lock, call requestPointerLock() on an element instead.

Next: prerendering

Learn how to detect Speculation Rules prerender with document.prerendering.

prerendering →

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.

6 people found this page helpful