The HTML ontouchend event
The HTML ontouchend attribute runs JavaScript when one or more touch points are removed from the touch surface. It is an inline handler for the touchend event; in modern code prefer addEventListener('touchend', …).
Overview
The ontouchend event attribute runs JavaScript when a touch ends on the element. In JavaScript the event itself is named touchend — drop the on prefix when you call addEventListener.
It is one of the Touch Events. The handler receives a TouchEvent whose touches list describes every active touch point, which is what makes multi-touch gestures possible. For most needs, though, the unified pointer events are now preferred over touch-specific handlers.
You can wire this up with the inline ontouchend HTML attribute, but the modern, recommended approach is element.addEventListener('touchend', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.
Syntax
<element ontouchend="handler()">…</element>
element.addEventListener('touchend', handler);
Best practices
- Prefer
element.addEventListener('touchend', handler)over the inlineontouchendattribute — it separates behavior from markup and allows multiple handlers. - Prefer the unified pointer events unless you specifically need multi-touch details.
- Iterate
event.touches(orchangedTouches) to handle each touch point. - Call
preventDefault()when you need to stop the browser's default scrolling or zooming.
Frequently asked questions
What is the ontouchend event?
touchend.Should I use touch events or pointer events?
How do I access the touch points?
event.touches list (or event.changedTouches) from the TouchEvent.Should I use the ontouchend attribute or addEventListener?
addEventListener('touchend', …) in JavaScript. The inline ontouchend attribute works but mixes behavior into the markup and allows only one handler per element.