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?
The dblclick() is an inbuilt method in jQuery which is used to trigger the double-click event to occur. This method occurs when the selected element will be double clicked. Syntax: $(selector).
The dblclick event fires when a pointing device button (such as a mouse's primary button) is double-clicked; that is, when it's rapidly clicked twice on a single element within a very short span of time. dblclick fires after two click events (and by extension, after two pairs of mousedown and mouseup events).
dblclick(function(e){ e. preventDefault(); }); }); Above jQuery code will disable double click event for all the controls on the webpage.
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>
Alternatively, here is the JSfiddle Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With