ontouchstart

The ontouchstart event in HTML is triggered when a touch point is placed on the touch surface, allowing interactive responses to touch interactions.

ontouchstart event

The ontouchstart event handler runs a script when a touch point is placed on the touch surface. It is commonly used to initiate touch interactions and gestures on mobile devices.

Syntax

In HTML

index.html
<element ontouchstart="myScript">

In JS

script.js
object.ontouchstart = myScript;

In JavaScript, the addEventListener() function is used.

script.js
object.addEventListener("touchstart", myScript);

Example

::event-ontouchstart ::
index.html
<!DOCTYPE html>
<html>
<body>
<h1>DOM touchstart Event</h1>
<p ontouchstart="myFunction()">Touch this paragraph to trigger a function that will write "Hello World".</p>
<p><strong>Note:</strong> This example is for touch devices only.</p>
<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

Values

Conclusion

The ontouchstart event in HTML is triggered when a touch point is placed on the touch surface, initiating the touch interaction. It is commonly used for starting gestures or interactions on mobile devices, such as tapping or swiping. This event can be leveraged to create responsive interfaces, enabling interactive features right when the user touches the screen.