jQuery on 'double click' event (dblclick for mobile)

I have the following jquery event handling function:

$('.target').on('dblclick', function() {
    //respond to double click event
});

My issue is that this event handler doesn't work on touch devices (iPhone, iPad...). Can anyone recommend a reliable alternative to dblclick that works on touch devices and still allows comfortable double click use on full size devices?

Answers


I ended up building a custom double click function that will work on both mobile and desktop:

var touchtime = 0;
$(".target").on("click", function() {
    if (touchtime == 0) {
        // set first click
        touchtime = new Date().getTime();
    } else {
        // compare first click to this click and see if they occurred within double click threshold
        if (((new Date().getTime()) - touchtime) < 800) {
            // double click occurred
            alert("double clicked");
            touchtime = 0;
        } else {
            // not a double click so set as a new first click
            touchtime = new Date().getTime();
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="target">Double click me</div>

You can bind multiple event listeners on the element and use jQuery's tap event for the touch devices.

$( ".target" ).on({
  dbclick: function() {
    //do stuff
  }, touch: function() {
    //do the same stuff
  }
});

Add this to your index.html

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

I found the mobile zoom function would throw off Jquery's dblclick. Basically it says your viewport wont change effectively shutting off the zoom. This works for me on my Nexus 5 running Chrome.


The marked answer of @JRulle seems to work only for a single object, if u have many instances with the same class they will be considered as a single object see the exampleFiddle example

My solution seems to work in cases like that

var touchtime = 0;
$('.target').on('click', function() {
  if (touchtime == 0) {
    touchtime = new Date().getTime();
  } else {
    if (((new Date().getTime()) - touchtime) < 800) {
      alert("double clicked");
      touchtime = 0;
    } else {
      touchtime = 0;
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p class="target">click me!</p>
<p class="target">then click me!</p>

<a href="#">click link</a>

Thanks for the solution - the only thing I did was add a timeout so that they could be treated as separate events

var touchtime = 0;
var delay = 800;
var action = null;
$(".target").on("click", function() {
  /*Double Click */
  if((new Date().getTime() - touchtime) < delay){
     clearTimeout(action)
     alert('dbl');
     touchtime=0;
  }
  /* Single Click */
  else{
     touchtime = new Date().getTime();
     action = setTimeout(function(){
        alert('single');
     },delay);
  }
}));

Although I haven't tested it, might also be worth adding the following to a header section of any HTML <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/> as per: To "user-scalable=no" or not to "user-scalable=no"


I have an improvement to the code above, that didnĀ“t detect a doubleclick after a single click:

var touchtime = 0;
$(".target").on("click", function() {
  if (((new Date().getTime()) - touchtime) < 500) {
    alert("double clicked");
  }
  touchtime = new Date().getTime();
});

This code detects all doubleclicks. I also reduced the touchtime to 500ms (standard doubleclick-time).


This is it... in CoffeeScript

onDblClick = -> "...your function to be fired..."
dbl_click = null
$(element).on 'mousedown', ->
   onDblClick() if dbl_click
   dbl_click = true
   setTimeout () ->
       dbl_click = false            
   , 250

You need to enter "return false" to the end of the function like below

var touchtime = 0;
$('.dbclickopen').click(function() {
    if(touchtime == 0) {
        //set first click
        touchtime = new Date().getTime();
    } else {
        //compare first click to this click and see if they occurred within double click threshold
        if(((new Date().getTime())-touchtime) < 800) {
            //double click occurred
            touchtime = 0;
            window.location = this.href;
        } else {
            //not a double click so set as a new first click
            touchtime = new Date().getTime();
        }
    }
    return false;
});

Need Your Help

Protractor, with isDisplayed() I get NoSuchElementError: No element found using locator

javascript angularjs jasmine protractor angularjs-e2e

In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:

How to catch and wrap exceptions thrown by JTA when a container-managed-tx EJB commits?

java-ee jpa ejb eclipselink jboss7.x

I'm struggling with a problem with an EJB3 class that manages a non-trivial data model. I have constraint validation exceptions being thrown when my container-managed transactional methods commit. ...