Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend jQuery's .on() to work with mobile touch events

I am attempting to use the jQuery mobile events without the rest of jQuery mobile.

https://github.com/jvduf/jquery-mobile-events/blob/master/jquery.mobile.events.js

That snippet enables them all, and works fine, but not with the .on() event handler. E.g:

$('a').on('tap',function(){
    console.log('Hi there!');
});

However it does work with .live(), but that is now depreciated.

So my question; is there a a way to extend the .on() functionality to include the tap event and others? Full list below:

  • touchstart
  • touchmove
  • touchend
  • orientationchange
  • tap
  • taphold
  • swipe
  • swipeleft
  • swiperight
  • scrollstart
  • scrollstop

Thanks :)

like image 943
will Avatar asked Feb 16 '12 12:02

will


People also ask

Does Safari support touch events?

Safari mobile doesn't support touch events. We have tried to fix it with mouse events as it was written in the apple documentation but had no success.

Is click event same as touch?

The touchstart event occurs when the user touches an element. But a click event is fired when the user clicks an element. Usually, both the touchstart and click events are fired in the very same click in the touch and click enabled devices.

Does touch trigger click event?

Because mobile browsers should also work with with web applications that were build for mouse devices, touch devices also fire classic mouse events like mousedown or click . When a user follows a link on a touch device, the following events will be fired in sequence: touchstart.

What is touch event in Javascript?

Touch events consist of three interfaces ( Touch , TouchEvent and TouchList ) and the following event types: touchstart - fired when a touch point is placed on the touch surface. touchmove - fired when a touch point is moved along the touch surface. touchend - fired when a touch point is removed from the touch surface.


1 Answers

However it does work with .live(), but that is now depreciated.

So I take it that you want to use event delegation to preserve those events on replaced elements. That would mean that this:

$('a').on('tap',function () {
    console.log('Hi there!');
});

would need to change to something like:

$(document).on('tap', 'a', function () {
    console.log('Hi there!');
});

in order for it to behave the same as $("a").live("tap", ...

like image 86
karim79 Avatar answered Oct 03 '22 17:10

karim79