ontouchstart
One or more touch contacts begin on the target. 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: TouchEvent. For the described native event: bubbles: yes; cancelable: varies. Use changedTouches for newly changed contacts; passive listeners cannot cancel default scrolling. Prefer pointer events for unified device handling. Example: Use a touch-enabled device; interruption events depend on the browser. 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
target.addEventListener("touchstart", event => { /* handle event */ });Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| event | TouchEvent | Event object delivered to the addEventListener callback; event.type is "touchstart". Inspect target, currentTarget, bubbles and cancelable as appropriate. |
Working Example
<button id="demo" type="button">Pointer test</button><p role="status" id="log">Waiting for touchstart</p><script> const target = document.getElementById("demo"); const log = document.getElementById("log"); target.addEventListener("touchstart", event => { log.textContent = String(event.type + ": " + event.changedTouches.length); });</script>