JavaScript Node selectstart Event

Beginner
6 min read
Updated: Oct 2026
5 examples
Limited

What Is selectstart?

The selectstart event of the Selection API fires when a user starts a new selection (MDN) — for example dragging to highlight text. Cancel it and the selection is not changed.

Limited availability: not Baseline — missing in some browsers (notably Safari on iOS). Not Deprecated, Experimental, or Non-standard. Prefer CSS user-select when you only need a style-level block.

Remember
document.addEventListener("selectstart", (e) => {
  // user began selecting
});

// block selection on this node / document:
e.preventDefault();  // selection is not changed

vs selectionchange → fires after selection updates (not cancelable)
vs getSelection()  → read the current Selection object
GoalPrefer
React when selection beginsselectstart
React when selection changesselectionchange
Read selected textgetSelection()

How to Use selectstart

Listen on document (or a container), then log or cancel.

  1. Add addEventListener("selectstart", …) (or set onselectstart).
  2. Drag across text to start a selection.
  3. Optionally call preventDefault() to keep the selection unchanged.
  4. Feature-test / test Safari iOS if blocking selection is critical.
PieceRequired?Role
Event nameYes"selectstart"
Handler propertyAltonselectstart
Event type—Generic Event
CancelOptionalpreventDefault()

Pseudocode

Pseudocode
on selectstart:
  if blocking this area:
      preventDefault()
      // selection stays as it was
  else:
      // selection may start / update
      // later: selectionchange may fire
      // read with getSelection()

Cheat Sheet

Keep these facts handy when working with text selection.

TopicDetail
ListenaddEventListener("selectstart", fn)
Propertynode.onselectstart = fn
TypeGeneric Event
CancelpreventDefault() → selection not changed
APISelection API
StatusLimited availability (not Baseline)
iOS SafariUnsupported — test if blocking matters

Live Preview

Drag across the sample text. Toggle allow vs block (preventDefault on selectstart).

Sample text

Drag to select this sentence — CodeToFun selectstart demo.

Browser demo of selectstart — Limited availability in some browsers.

Result
Choose allow or block, then drag to select…

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Log When Selection Starts

MDN-style listener on document.

JavaScript
document.addEventListener("selectstart", () => {
  console.log("Selection started");
});
Try It Yourself

How It Works

  1. Listen on document. Catch selection starts anywhere in the page.
  2. Drag across text. The browser fires selectstart.
  3. Log once per start. A new drag starts a new selection.
  4. No cancel here. Selection proceeds normally.

Example 2 — onselectstart Property

Same idea using the handler property (MDN).

JavaScript
document.onselectstart = () => {
  console.log("Selection started.");
};
Try It Yourself

How It Works

  1. Assign the property. Only one onselectstart handler at a time.
  2. Prefer addEventListener when you need multiple listeners.
  3. Same event. Still fires when a new selection begins.
  4. Clear with null. document.onselectstart = null removes it.

Example 3 — Block Selection

Cancel the event so the selection does not change (MDN).

JavaScript
const panel = document.getElementById("no-select");

panel.addEventListener("selectstart", (e) => {
  e.preventDefault();
  console.log("selection blocked");
});
Try It Yourself

How It Works

  1. Listen on the panel. Only that subtree is watched here.
  2. Call preventDefault. MDN: selection is not changed.
  3. Log for learning. Confirms the handler ran.
  4. iOS caveat. May not fire — CSS user-select: none as backup.

Example 4 — Allow Only One Zone

Block document selection except inside a readable area.

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

document.addEventListener("selectstart", (e) => {
  if (!allowed.contains(e.target)) {
    e.preventDefault();
  }
});
Try It Yourself

How It Works

  1. Listen on document. Catch every selection start.
  2. Check the target. Is it inside the allowed node?
  3. Cancel outside. preventDefault only when not allowed.
  4. Keep copyable content. Useful for apps with mixed UI + article text.

Example 5 — selectstart vs selectionchange

Start is cancelable; change reports updates via getSelection().

JavaScript
document.addEventListener("selectstart", () => {
  console.log("selectstart");
});

document.addEventListener("selectionchange", () => {
  const text = document.getSelection().toString();
  console.log("selectionchange:", JSON.stringify(text));
});
Try It Yourself

How It Works

  1. selectstart first. Selection is beginning.
  2. selectionchange next. Range updates as you drag.
  3. Read with getSelection. The change event does not carry the text.
  4. Different jobs. Cancel start vs observe changes.

Edge Cases & Pitfalls

Check these before relying on selectstart alone.

limited

Not Baseline

MDN: Limited availability. Safari iOS does not support selectstart.

cancel

Cancel blocks the new selection

MDN: if canceled, the selection is not changed. Pair with preventDefault().

css

Prefer user-select for style-only blocks

For “no highlight” UI chrome, CSS user-select: none is simpler and more portable.

vs change

Not the same as selectionchange

Use selectionchange + getSelection() to read updates.

Browser Support

Node selectstart is marked Limited availability (not Baseline) on MDN. Desktop Chromium, Firefox, and Safari generally support it; Safari on iOS does not.

✓ Limited · Not Baseline

Node selectstart

Fires when a user starts a new selection. Cancel with preventDefault to keep the selection unchanged. Test iOS Safari if blocking selection is critical.

Limited Not Baseline
Google Chrome Supported · Desktop & Android
Supported
Mozilla Firefox Supported · Desktop & Android
Supported
Microsoft Edge Supported · Chromium
Supported
Apple Safari Desktop yes · iOS no
Partial
Opera Supported · Modern versions
Supported
Samsung Internet Supported · Android
Supported
selectstart Limited

Bottom line: Fine for progressive enhancement. Prefer CSS user-select when you only need a style-level block, and always verify on iOS.

Performance Notes

ApproachCostBest for
selectstart listenerPer selection startGate / log begins
selectionchangeMay fire often while draggingLive selected-text UI
CSS user-selectNo JSSimple no-select regions

selectstart is cheap (once per new selection). Avoid heavy work on every selectionchange while the user is still dragging.

Key Takeaways

  • Fires on start: user begins a new selection.
  • Cancelable: preventDefault() keeps selection unchanged.
  • Limited: not Baseline — weak on Safari iOS.
  • Pair: selectionchange + getSelection() to read text.

One line: selectstart is the cancelable “selection began” signal from the Selection API.

Frequently Asked Questions

It is a Selection API event fired when a user starts a new selection (for example dragging to highlight text). If you cancel the event, the selection is not changed (MDN).
No. MDN does not mark Node selectstart as Deprecated, Experimental, or Non-standard. It is labeled Limited availability (not Baseline) because support is missing in some browsers—notably Safari on iOS.
A generic Event. Cancel it with event.preventDefault() to block the new selection from starting.
Use addEventListener("selectstart", handler) or the onselectstart property on document, window, or an element that participates in selection.
selectstart fires when a selection begins (and can be canceled). Document selectionchange fires whenever the selection changes and is not cancelable — use getSelection() inside that listener.
Compatibility data show selectstart unsupported on Safari iOS. Desktop Safari supports it. Always test on your target devices if blocking selection matters.

Next: baseURI

Learn the Node baseURI property.

baseURI tutorial →

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