This post is regularly updated with new, small code snippets that I’d like to share with you, but are too small to require an entire article.

jQuery Doubleclick & tap

This jQuery script enables doubleclick and doubletap (mobile) behaviour, without the need for global variables. It does not rely on jQuery’s ‘dblclick’ handler, but needs to be binded to the regular ‘click’ event.


$(document).on('click', '#MyDoubleClickElement', function () {
  var t = $(this), doubleClickInterval = 500; //set up base vars
  var lastTouch = t.data('lastTouch') || 0, time = new Date().getTime(); //check when this element has been clicked last
  t.data('lastTouch', time); //store this click time
  if (time - lastTouch < doubleClickInterval && lastTouch !== 0) { //check if time between this and previous click exceeds the threshhold. If there is no last click registered, don't handle the callback
    //do your stuff here (execute a callback)
    alert("Double click!");
  }
});

Test it for yourself in this Fiddle