changing onclick to touchstart

Associate
Joined
11 Oct 2008
Posts
268
Hey,
I have been using this snippet of code to fire a function when a div is clicked.

Code:
<div class="top" onclick="moveUp()"></div>

Is it possible to change the onclick so it works with touchstart ?
 
You can simply change the "onclick" attribute to "ontouchstart".

If you wanted to separate your markup from your JS, you can bind event handlers like this:

var divEl = document.querySelector(".top"); // assumes our wanted element is the first one with the class "top"
if (divEl) divEl.addEventListener("touchstart", moveUp); // moveUp will be passed the event object
 
Back
Top Bottom