1

Given an HTML page full of onclicks and more being added by JavaScript later on, how would one make these functions fire on touch events on iOS as well?

For example:

<div onclick='dosomething();'></div>

Preferably just one piece of JS that runs through the page add touch events. JQuery is fine.

Laef
  • 902
  • 2
  • 8
  • 24
  • [You should avoid the `onClick` attributes in your html](http://stackoverflow.com/a/12627478/4202224). While using jQuery use the `.on()` or `.click()` method. So you can use `.on('click touch')` to register event-handler – empiric Jul 06 '16 at 19:27

1 Answers1

-1

Check this out

MDN Touch events

Add the event handlers like so.

<canvas id="canvas" width="600" height="600" style="border:solid black 1px;">
  Your browser does not support canvas element.
</canvas>
<br>
<button onclick="startup()">Initialize</button>
<br>
Log: <pre id="log" style="border: 1px solid #ccc;"></pre>

-

function startup() {
  var el = document.getElementsByTagName("canvas")[0];
  el.addEventListener("touchstart", handleStart, false);
  el.addEventListener("touchend", handleEnd, false);
  el.addEventListener("touchcancel", handleCancel, false);
  el.addEventListener("touchmove", handleMove, false);
  log("initialized.");
}

Then you can create the functions handleStart, handleEnd etc to do what ever.

Pieter de Vries
  • 777
  • 5
  • 15