HTML ontouchcancel Event Attribute


The ontouchcancel HTML event attribute is used to specify a function to be executed when a touch event is interrupted. This can occur for a variety of reasons, such as:

  • The user removes their finger from the touch surface.
  • The user puts down too many fingers on the touch surface.
  • The user interacts with a non-touch element on the page.
  • The touch event is canceled by the user agent.

The ontouchcancel event is typically used to clean up any state that was created by the touch event. For example, if a touch event is used to start a drag-and-drop operation, the ontouchcancel event can be used to cancel the drag-and-drop operation.

The ontouchcancel event can be specified in HTML using the ontouchcancel attribute, or in JavaScript using the addEventListener() method.

For example, the following HTML code would specify a function to be executed when a touch event is canceled on the <div> element:

<div ontouchcancel="myFunction()">...</div>

The following JavaScript code would do the same thing:

document.querySelector('div').addEventListener('touchcancel', myFunction);

In both cases, the myFunction() function would be executed when a touch event is canceled on the <div> element.


<element ontouchcancel="script">


  • scriptThe name of the script to use when the event has been triggered.


<p ontouchcancel="myFunction()">Touch this paragraph while you do something that will interrupt the event. Different devices will interrupt the touch at different actions, so this example can be difficult to demonstrate, but it is considered good practice to include this event.</p>

<p><strong>Note:</strong> This example is for touch devices only.</p>

<p id="demo"></p>

function myFunction() {
document.getElementById("demo").innerHTML = "Touch Cancelled";

Browser Support

The following table will show you the current browser support for the HTML ontouchcancel Event Attribute.

Edge Chrome Firefox Opera Safari
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview

Last updated by CSSPortal on: 21st October 2023