Search code examples
javascriptjquerytouchtouch-event

Simulating touchstart and touchend events?


I'm developing a jquery component which works primarily for ipad. So is there anyway to simulate 'touchstart and 'touchend' events in desktop rather than having the device itself to check the events.


Solution

  • You can author your own custom events within jQuery:

    var event = $.Event( "touchstart", { pageX:200, pageY:200 } );
    

    And you can issue them against any element in the DOM:

    $("body").trigger( event );
    

    Demo: http://jsbin.com/ezoxed/edit#javascript,html
    Further reading: http://api.jquery.com/category/events/event-object/

    Keep in mind that there are various other types of interfaces on the market now that don't support touchstart and touchend events. For instance, Windows 8 is already occupying tablets in the mobile market, and it uses a more abstracted event model consisting of Pointers.