1

I would like to get the touchstart and touchend locations if element "#menu" is touched.

I wrote this code, but the event doesn't seem to trigger at all:

        var lastLoc = 0;

        $(document).on('touchstart', '#menu', function(e) {
            lastLoc = e.originalEvent.touches[0].pageX;
        });

        $(document).on('touchend', '#menu', function(e) {
            var newLoc = e.originalEvent.touches[0].pageX;
            alert("lastLoc = "+lastLoc+", newLoc = "+newLoc);
        });

As far as I know this should work.

Thanks in advance.

3 Answers3

2

The touchend event should, logically, have zero touch points, only changedTouches.

Here's a demo.

function startHandler (e) {
    window.console.log('Start:', e.originalEvent.touches[0].pageX);
}

function endHandler (e) {
    window.console.log('End:', e.originalEvent.changedTouches[0].pageX);
}



$(document).on('touchstart', '#menu', startHandler);
$(document).on('touchend', '#menu', endHandler);

See also: Simulating touchstart and touchend events? if you are having trouble testing touch events.

Community
  • 1
  • 1
Oka
  • 23,367
  • 6
  • 42
  • 53
0

You need to test the touch event on a touchable surface such as a smartphone or a tablet. In Google Chrome there is a way to simulate this function.

julio
  • 2,762
  • 4
  • 22
  • 34
0

Sorry for your trouble, I ended up using jquery mobile's "swipeleft" Thanks anyway :)