Events

onselectstart

A new text selection is about to begin. Event defined by a related web API or specification, not simply a portable HTML global handler. Use addEventListener on the actual target; inline handler exposure and browser support may differ. Event interface: Event. For the described native event: bubbles: yes; cancelable: yes. Canceling prevents the selection; preserve normal text selection unless there is a strong interaction reason. Example: Start selecting the text. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.

Formal Syntax & Structure

html syntaxhtml
target.addEventListener("selectstart", event => { /* handle event */ });

Parameters & Attributes

NameTypeDescription
eventEventEvent object delivered to the addEventListener callback; event.type is "selectstart". Inspect target, currentTarget, bubbles and cancelable as appropriate.

Working Example

Example Snippethtml
<p id="demo">Select some words in this sentence.</p><p role="status" id="log">Waiting for selectstart</p><script>  const target = document.getElementById("demo");  const log = document.getElementById("log");  target.addEventListener("selectstart", event => {    log.textContent = String(event.type);  });</script>
HTML Reference