JavaScript Element beforexrselect Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Experimental
Instance event

What You’ll Learn

The beforexrselect event fires before WebXR select / selectstart / selectend events. Learn how DOM overlays use it, why preventDefault() matters, how XRSessionEvent exposes session, and how to feature-detect support—with five examples and try-it labs.

01

Kind

Instance event

02

Type

XRSessionEvent

03

Cancelable

Yes (bubbles)

04

Status

Experimental

05

Module

WebXR DOM Overlays

06

Suppresses

XR select*

Introduction

WebXR sessions can show a DOM overlay—HTML UI composited into the XR experience (menus, buttons, forms). When the user aims a controller or hand ray at that UI, you usually want normal DOM interaction, not a second “select” in the XR world.

beforexrselect is the bridge: it fires first, and if you cancel it on the overlay, the browser skips the XR selectstart / select / selectend sequence for that action. DOM pointer events still work separately.

💡
Beginner tip

Think of it as: “UI click coming—please do not also fire XR select.” Listen on the overlay root and call preventDefault().

Understanding beforexrselect

An instance event used with WebXR DOM overlays. It answers: “Is an XR select about to be generated for this interaction?”

  • Fires before WebXR select events are dispatched.
  • Bubbles, cancelable, composed (MDN).
  • XRSessionEvent — read event.session.
  • Purpose — suppress XR world input while using overlay UI.
  • Status — Experimental; Limited availability (not Baseline).

📝 Syntax

Use the event name with addEventListener, or set the handler property:

JavaScript
addEventListener("beforexrselect", (event) => { });

onbeforexrselect = (event) => { };

Event type

An XRSessionEvent (inherits from Event).

Event properties

PropertyMeaning
sessionRead-only XRSession this event refers to

Event flags

MDN: the event bubbles, is cancelable, and is composed.

🔁 Overlay vs XR Select

  1. User triggers a primary XR action aimed at the DOM overlay.
  2. Browser fires beforexrselect on the intersected overlay target (bubbles).
  3. If you call preventDefault(), XR select* events are suppressed for that sequence.
  4. DOM UI events (such as pointer events) can still occur independently.

That separation avoids “double input”: clicking a menu button should not also select an object in the XR scene.

⚖️ beforexrselect vs XR select

TopicbeforexrselectXR select / selectstart / selectend
TimingBefore XR select eventsThe XR select sequence itself
Typical targetDOM overlay elementXRSession / input pipeline
Cancel effectCan suppress the XR select sequenceN/A if suppressed upstream
Best forProtecting HTML UI from XR picksSelecting objects in the XR world

⚡ Quick Reference

GoalCode / note
Listen on overlayoverlay.addEventListener("beforexrselect", fn)
Handler propertyoverlay.onbeforexrselect = fn
Suppress XR select*event.preventDefault()
Read sessionevent.session
Feature-detect"onbeforexrselect" in window / element (weak) + WebXR session
MDN statusExperimental ยท Limited availability

🔍 At a Glance

Four facts to remember about beforexrselect.

Event type
XRSessionEvent

Has session

Cancelable
yes

Bubbles too

Suppresses
select*

XR input

Baseline?
no

Limited avail.

Examples Gallery

Examples follow MDN Element: beforexrselect event. Full XR sessions need a headset or emulator; labs still teach registration, cancel, and detection patterns.

📚 Getting Started

Register handlers on a DOM overlay root.

Example 1 — addEventListener("beforexrselect")

Attach a listener to the overlay container (MDN style).

JavaScript
const overlay = document.getElementById("xr-overlay");

overlay.addEventListener("beforexrselect", (event) => {
  console.log("beforexrselect on overlay");
  console.log("bubbles:", event.bubbles, "cancelable:", event.cancelable);
});
Try It Yourself

How It Works

Prefer the overlay root so bubbled events from nested buttons and panels are covered by one listener.

Example 2 — onbeforexrselect Property

Same idea using the event handler property.

JavaScript
const overlay = document.getElementById("xr-overlay");

overlay.onbeforexrselect = (event) => {
  console.log("onbeforexrselect fired");
};

// Assigning again replaces the previous handler.
Try It Yourself

How It Works

Prefer addEventListener when you need multiple listeners or easy removal.

📈 Suppress, Session & Detect

Cancel XR select for UI hits, inspect session, and feature-detect.

Example 3 — Suppress XR Select with preventDefault()

MDN pattern: cancel on the overlay so XR select events do not fire for that UI interaction.

JavaScript
document
  .getElementById("xr-overlay")
  .addEventListener("beforexrselect", (ev) => ev.preventDefault());

// Effect (supporting browsers, during XR + DOM overlay):
// selectstart / select / selectend are not dispatched for this action.
// DOM UI interaction can continue without duplicate XR world input.
Try It Yourself

How It Works

Because the event bubbles, one cancel on the overlay root protects nested interactive controls inside that container.

Example 4 — Read event.session

Use the XRSessionEvent session reference when you need session context.

JavaScript
const overlay = document.getElementById("xr-overlay");

overlay.addEventListener("beforexrselect", (event) => {
  const session = event.session;
  console.log("XRSession present:", !!session);
  // Optionally inspect session.renderState, inputSources, etc.
  event.preventDefault();
});
Try It Yourself

How It Works

session ties the DOM overlay event back to the active immersive session so you can coordinate UI state with XR state.

Example 5 — Feature Detection

Check for WebXR and the handler surface before relying on the event.

JavaScript
function isBeforeXrSelectLikelyAvailable() {
  return (
    "xr" in navigator &&
    ("onbeforexrselect" in window || "onbeforexrselect" in HTMLElement.prototype)
  );
}

console.log("WebXR navigator.xr:", "xr" in navigator);
console.log("beforexrselect likely:", isBeforeXrSelectLikelyAvailable());

if (isBeforeXrSelectLikelyAvailable()) {
  document.getElementById("xr-overlay")?.addEventListener("beforexrselect", (e) => {
    e.preventDefault();
  });
} else {
  console.log("Plan a non-overlay or non-XR UI fallback");
}
Try It Yourself

How It Works

Handler-property checks are imperfect. Always verify DOM overlay + select behavior on your target XR runtime.

🚀 Common Use Cases

  • Menus and settings panels inside a WebXR DOM overlay.
  • Preventing XR world picks while pressing HTML buttons.
  • Forms or confirmation dialogs composited over an immersive session.
  • Teaching the difference between DOM pointer input and XR select input.
  • Feature-gated XR UI that falls back when overlays are unsupported.

🔧 How It Works

1

User aims at DOM overlay

Primary XR action intersects overlay HTML.

Aim
2

beforexrselect fires

An XRSessionEvent bubbles from the overlay target.

Event
3

Your handler may cancel

preventDefault() suppresses XR select* for this action.

Decide
4

UI wins without duplicate XR pick

DOM interaction proceeds; XR world select stays quiet.

📝 Notes

  • MDN: Experimental and Limited availability (not Baseline).
  • Not Deprecated or Non-standard — only the Experimental banner is shown.
  • Bubbles / cancelable / composed — cancel on the overlay root for nested UI.
  • DOM events and XR events are not synchronized; canceling XR select does not block DOM pointers.
  • Related learning: beforescriptexecute, activeVRDisplays, addEventListener(), JavaScript hub.

Limited Browser Availability

beforexrselect is Experimental and not Baseline on MDN. Logos use the shared browser-image-sprite.png sprite from this project. Expect Chromium-family support for WebXR DOM overlays; Firefox and Safari generally lack this event.

Experimental · Limited availability

Element beforexrselect

Use with WebXR DOM overlays to suppress XR select* while interacting with HTML UI. Always feature-detect and test on device.

Limited Not Baseline
Google Chrome 83+ · WebXR DOM overlays
Supported
Mozilla Firefox No beforexrselect support
Unavailable
Apple Safari No beforexrselect support
Unavailable
Microsoft Edge 83+ Chromium · WebXR overlays
Supported
Opera 69+ · Chromium-based
Supported
Internet Explorer No WebXR / beforexrselect
Unavailable
beforexrselect Limited

Bottom line: Listen on the DOM overlay, call preventDefault() to protect UI, and keep a fallback for browsers without WebXR overlays.

Conclusion

beforexrselect lets WebXR DOM overlays claim a controller or hand action for HTML UI before XR select events fire. Cancel it on the overlay root, keep an eye on limited browser support, and always test in a real immersive session.

Continue with blur, addEventListener(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Prefer addEventListener("beforexrselect", ...) on the overlay root
  • Call preventDefault() when UI should own the interaction
  • Feature-detect WebXR + event support
  • Test on real XR hardware or a trusted emulator
  • Keep a non-XR / non-overlay UI fallback

❌ Don’t

  • Assume Firefox or Safari support today
  • Expect canceling XR select to stop all DOM pointer events
  • Ship without checking Limited availability on MDN
  • Confuse this with ordinary mouse select / text selection
  • Overwrite onbeforexrselect if you need multiple listeners

Key Takeaways

Knowledge Unlocked

Five things to remember about beforexrselect

Cancel on the DOM overlay so XR select* does not fight your HTML UI.

5
Core concepts
🧪 02

Experimental

limited avail.

Status
🛑 03

Cancelable

preventDefault

API
📄 04

XRSessionEvent

event.session

Type
🚀 05

Suppress select*

protect UI

Goal

❓ Frequently Asked Questions

It fires before WebXR select events (select, selectstart, selectend) are dispatched. Apps use it on a DOM overlay to suppress XR world input while the user interacts with HTML UI.
No. MDN marks it Experimental and Limited availability (not Baseline). It is part of the WebXR DOM Overlays Module. It is not Deprecated or Non-standard.
An XRSessionEvent (inherits from Event). It exposes a read-only session property referring to the XRSession.
Yes. MDN states beforexrselect bubbles, is cancelable, and is composed. Calling preventDefault() on the overlay (or a container) suppresses the following XR selectstart / select / selectend for that interaction.
Support is limited. Chromium-based browsers (Chrome / Edge from around 83+) implement it. Firefox and Safari generally do not. Always feature-detect and test on XR hardware.
overlay.addEventListener("beforexrselect", handler) or overlay.onbeforexrselect = handler. Listen on the DOM overlay element so bubbling can cover its UI tree.
Did you know?

Cancelling beforexrselect only suppresses the WebXR select sequence. The same user action can still produce ordinary DOM events (for example pointerdown), and those may arrive before or after beforexrselect—they are not locked together.

Next: blur

Learn the event that fires when an element loses focus.

blur →

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.

5 people found this page helpful