javascriptandroidjquerygoogle-chrometouchstart

No original event on touchstart


So i'm using JQuery (1.11.3) and I've been stuck on something for which I can't find anything on the web.

Basically, I'm binding a handler to a 'touchstart' event and want to retrieve the position of the touch event. (note: I also tried with $('#id').on('touchstart'... )

$(document).bind('touchstart','#main-button', function( e ){
    alert(Object.keys(e).join('\n'));
    alert(Object.keys(e.originalEvent).join('\n'));
    // awesome code here
})

This is what I get from the first alert:

screenshot android 1 screenshot android 2

The problem is that the originalEvent (which is supposed to have the information I'm looking for) only has the property isTrusted.

screenshot android 3

Does anyone know what I am doing wrong ? I'm using Chrome for Android by the way.


Solution

  • The originalEvent does have other properties (see MDN reference for touchstart event property list). Object.keys is just not returning them. This is because either the properties are not enumerable or they are defined somewhere in the prototype chain.

    Instead you can use a for...in loop to get any enumerable properties on the object and those in the prototype chain.

    $(document).bind('touchstart','#main-button', function( e ){
        alert(Object.keys(e).join('\n'));
        var keys = "";
        for(key in e.originalEvent){
           keys+=key+"\n";
        }
        alert(keys);
        // awesome code here
    })