Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make ondblclick event works on phone?

I want to achieve the double click event on a specific div like this:

<div   id="divID" ondblclick = 'alert("double click!!");' >

it worked on the google chrome browser but when I open it with phone it didn't work, by the way the single click worked.

ps: i added this two things

<meta name="viewport" content="width=device-width, initial scale=1,user-scalable=no">

and this

body {
-ms-touch-action: manipulation;
touch-action: manipulation;}

but it didnt work!

like image 266
Wissem Achour Avatar asked Mar 09 '15 11:03

Wissem Achour


People also ask

What is a Ondblclick event?

The ondblclick event occurs when the user double-clicks on an element.

How do I trigger a double click event in react?

To handle double click events in React:Add an onClick prop to the element. Use the detail property on the event object to get the click count. If the click count is equal to 2, handle the double click event.

Is there a double click event in JavaScript?

No it isn't. There is a dblclick event but no corresponding shortcut function.


2 Answers

I got the same issue. On touch devices, if you want to detect a double-tap gesture and you use the ondblclick event in most cases it will not work and also the problem is it will also fire an onclick. One of the solution is to implement a double tap detection pattern using the following code sample:

var doubletapDeltaTime_ = 700;
var doubletap1Function_ = null;
var doubletap2Function_ = null;
var doubletapTimer = null;

function tap(singleTapFunc, doubleTapFunc) {
    if (doubletapTimer==null) {
    // First tap, we wait X ms to the second tap
        doubletapTimer_ = setTimeout(doubletapTimeout_, doubletapDeltaTime_);
        doubletap1Function_ = singleTapFunc;
        doubletap2Function_ = doubleTapFunc;
    } else {
    // Second tap
        clearTimeout(doubletapTimer);
        doubletapTimer_ = null;
        doubletap2Function_();
    }
}

function doubletapTimeout() {
// Wait for second tap timeout
    doubletap1Function_();
    doubleTapTimer_ = null;
}

And you can call it like

<div   id="divID" onclick="tap(tapOnce, tapTwice)" >

tapOnce and tapTwice are your functions which will be called in respective cases. This solution will work on browsers too.

Reference

like image 59
planet260 Avatar answered Sep 22 '22 23:09

planet260


Here is the external function 'doubletap' which can be helpful:

/*
 * jQuery Double Tap
 * Developer: Sergey Margaritov ([email protected])
 * Date: 22.10.2013
 * Based on jquery documentation http://learn.jquery.com/events/event-extensions/
 */

(function($){

  $.event.special.doubletap = {
    bindType: 'touchend',
    delegateType: 'touchend',

    handle: function(event) {
      var handleObj   = event.handleObj,
          targetData  = jQuery.data(event.target),
          now         = new Date().getTime(),
          delta       = targetData.lastTouch ? now - targetData.lastTouch : 0,
          delay       = delay == null ? 300 : delay;

      if (delta < delay && delta > 30) {
        targetData.lastTouch = null;
        event.type = handleObj.origType;
        ['clientX', 'clientY', 'pageX', 'pageY'].forEach(function(property) {
          event[property] = event.originalEvent.changedTouches[0][property];
        })

        // let jQuery handle the triggering of "doubletap" event handlers
        handleObj.handler.apply(this, arguments);
      } else {
        targetData.lastTouch = now;
      }
    }
  };

})(jQuery);
like image 45
Wissem Achour Avatar answered Sep 23 '22 23:09

Wissem Achour