2

I'm trying to find event that will be fired on desktop click event (mouse or touch pad) but will not fire on mobile device with "touchstart" event. I need this because in this code

$('#' + id).on("touchstart mousedown", function(event) {
    some actions
}

my actions will fire twice on mobile devices. First time on "touchstart" and after that on "mousedown". Now i'm hacking like that:

var block = $(this);
if (event.type === "touchstart" && "mousedown" in $._data( block[0], "events" )) {
    $(block).off('mousedown');
}

But this is so ugly.

Is there any clear way to catch separate "touchstart" event on mobile device and something like "click" or "mousedown" that will fire only on desktop?

UPDATE. Hmm i think i found pretty answer on my question. I can detect 'ontouchstart' presence, determine device and bind events only after that. Thank for links!

if ('ontouchstart' in document.documentElement) {
  //...
}
valex
  • 5,163
  • 2
  • 33
  • 40
  • check for `event.type`.. like `$( "a" ).click(function( event ) { alert( event.type ); // "click" });` – guradio Mar 28 '17 at 09:11
  • @guradio You mean exactly like they're already doing? – JJJ Mar 28 '17 at 09:14
  • 1
    Please, check answer in below link, http://stackoverflow.com/questions/25522955/jquery-on-touchstart-mousedown-both-events-are-triggered-on-touchscreen-devi – Prabu Mar 28 '17 at 09:21

0 Answers0