views:

324

answers:

1

While this code produces the expected behavior of "1" when touching the screen:

document.getElementById('someNodeId').addEventListener('touchmove', touch, true);

function touch(evt) {
  evt.preventDefault();
  alert(evt.changedTouches.length);     
  }

the same code using a jQuery selector:

 $('#someNodeId').bind('touchmove', touch);

produces the error: "TypeError: Result of expression 'evt.changedTouches'[undefined] is not an object".

(Device = iPod Touch OS 3.1.3 (7E18); jQuery 1.4.2).

How is this possible and what am I doing wrong?

A: 

Try

$(document).ready (function () {
    $("#someNodeId").bind("touchmove", function (event) {
        var e = event.originalEvent;
        console.log(e.targetTouches[0].pageX);
    });
});
Fred Bergman
@Fred - an explanation of why `originalEvent` is needed will be helpful to OP and others who are not familiar with jQuery's event normalization.
Anurag
Indeed; the explanation is found here:http://stackoverflow.com/questions/671498/jquery-live-removing-iphone-touch-event-attributes"In order to 'fix' events, jQuery clones the event. In doing so, it only copies over a limited number of properties for performance reasons. However, you can still access the original event object via the event.originalEvent property."In fact I'm not sure it's very useful to use jQuery on iPhone; I went this route because of jQTouch but now I'm moving away from it.