Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind 'touchstart' and 'click' events but not respond to both?

People also ask

What is the counterpart to the Touchstart event?

Definition and Usage Tip: Other events related to the touchstart event are: touchend - occurs when the user removes the finger from an element. touchmove - occurs when the user moves the finger across the screen. touchcancel - occurs when the touch is interrupted.

What is Touchstart click?

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 Mousedown fire on touch?

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 .

Does Touchstart work on desktop?

onTouchStart works only for mobile. Also, this event goes before the onClick event.


Update: Check out the jQuery Pointer Events Polyfill project which allows you to bind to "pointer" events instead of choosing between mouse & touch.


Bind to both, but make a flag so the function only fires once per 100ms or so.

var flag = false;
$thing.bind('touchstart click', function(){
  if (!flag) {
    flag = true;
    setTimeout(function(){ flag = false; }, 100);
    // do something
  }

  return false
});

This is the fix that I "create" and it take out the GhostClick and implements the FastClick. Try on your own and let us know if it worked for you.

$(document).on('touchstart click', '.myBtn', function(event){
        if(event.handled === false) return
        event.stopPropagation();
        event.preventDefault();
        event.handled = true;

        // Do your magic here

});

You could try something like this:

var clickEventType=((document.ontouchstart!==null)?'click':'touchstart');
$("#mylink").bind(clickEventType, myClickHandler);

Usually this works as well:

$('#buttonId').on('touchstart click', function(e){
    e.stopPropagation(); e.preventDefault();
    //your code here

});