99

Is there something in jquery that would allow me to differentiate between behavior on double click and single click?

When I bind both to same element only the single click gets executed.

Is there a way that wait for some time before execution of the single click to see if the user clicks again or not?

Thanks :)

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
kritya
  • 3,312
  • 7
  • 43
  • 60
  • 1
    http://stackoverflow.com/questions/1472433/how-can-jquery-be-used-to-handle-timer-in-click-dblclick-separation – Haim Evgi Jun 13 '11 at 12:37

15 Answers15

147

I found that John Strickler's answer did not quite do what I was expecting. Once the alert is triggered by a second click within the two-second window, every subsequent click triggers another alert until you wait two seconds before clicking again. So with John's code, a triple click acts as two double clicks where I would expect it to act like a double click followed by a single click.

I have reworked his solution to function in this way and to flow in a way my mind can better comprehend. I dropped the delay down from 2000 to 700 to better simulate what I would feel to be a normal sensitivity. Here's the fiddle: http://jsfiddle.net/KpCwN/4/.

Thanks for the foundation, John. I hope this alternate version is useful to others.

var DELAY = 700, clicks = 0, timer = null;

$(function(){

    $("a").on("click", function(e){

        clicks++;  //count clicks

        if(clicks === 1) {

            timer = setTimeout(function() {

                alert("Single Click");  //perform single-click action    
                clicks = 0;             //after action performed, reset counter

            }, DELAY);

        } else {

            clearTimeout(timer);    //prevent single-click action
            alert("Double Click");  //perform double-click action
            clicks = 0;             //after action performed, reset counter
        }

    })
    .on("dblclick", function(e){
        e.preventDefault();  //cancel system double-click event
    });

});
agrublev
  • 748
  • 1
  • 8
  • 22
Garland Pope
  • 3,242
  • 1
  • 25
  • 19
  • 1
    Use .delegate() over .live() though. – John Strickler Nov 03 '11 at 18:08
  • 24
    use `.on()` over both of those now – Claudiu Sep 04 '12 at 15:40
  • V. Nice. Suggest readers play with the delay time, as it makes the single click action appear more sluggish than it actually is... of course this has to balanced with slow double clickers (lol) – MindWire Nov 25 '12 at 03:37
  • I too use .on() and set both click and double click events to call the same function - perfect solution for my use case. – alQemist Sep 23 '14 at 10:22
  • What is "#a" in this example and do I need to use on()? – Kala J Dec 30 '14 at 21:46
  • 2
    Using this solution delays the click event in 700ms! It is very annoying to user... – uriel Jun 25 '15 at 05:39
  • 1
    @uriel, if you think 700 ms is too long to wait for a double click, just make the number smaller until it suits your preference. – Garland Pope Jul 17 '15 at 20:32
  • 1
    as of jQuery 1.7: In order to support later-on loaded content (ajax stuff) use `$("document").on("click","a" function(e){` instead in line 5. This construct replaces deprecated `delegate()` usage. Instead of `document` you can/should use any parent object of the `a` down the DOM as far as possible that persists trough time. – Hafenkranich Oct 20 '16 at 17:41
  • 500ms is the default timeout in Windows. – Professor of programming May 29 '18 at 11:06
  • Instead of hard-coding the timing for what counts as a double-click, you can use the user's system setting (in case they changed it from the default) by adapting this answer for left-click instead of right: https://stackoverflow.com/a/22574495/1024735 – kevinmicke Jun 10 '19 at 14:03
15

The solution given from "Nott Responding" seems to fire both events, click and dblclick when doubleclicked. However I think it points in the right direction.

I did a small change, this is the result :

$("#clickMe").click(function (e) {
    var $this = $(this);
    if ($this.hasClass('clicked')){
        $this.removeClass('clicked'); 
        alert("Double click");
        //here is your code for double click
    }else{
        $this.addClass('clicked');
        setTimeout(function() { 
            if ($this.hasClass('clicked')){
                $this.removeClass('clicked'); 
                alert("Just one click!");
                //your code for single click              
            }
        }, 500);          
    }
});

Try it

http://jsfiddle.net/calterras/xmmo3esg/

Wictor Chaves
  • 957
  • 1
  • 13
  • 21
calterras
  • 308
  • 2
  • 4
10

Sure, bind two handlers, one to click and the other to dblclick. Create a variable that increments on every click. then resets after a set delay. Inside the setTimeout function you can do something...

var DELAY = 2000,
    clicks = 0,
    timer = null;

$('a').bind({
    click: function(e) {
        clearTimeout(timer);

        timer = setTimeout(function() {
            clicks = 0;
        }, DELAY);

        if(clicks === 1) {
            alert(clicks);
             //do something here

            clicks = 0;
        }

        //Increment clicks
        clicks++;
    },
    dblclick: function(e) {
        e.preventDefault(); //don't do anything
    }
});
John Strickler
  • 25,151
  • 4
  • 52
  • 68
  • Thanks a lot. I tried to add the if(clicks==1) but it didnt can u please add the the single click function. Again thanks :) – kritya Jun 13 '11 at 12:55
  • @kritya I edited my answer - this should work better for you. See fiddle here - http://jsfiddle.net/VfJU4/1/ – John Strickler Jun 13 '11 at 13:13
  • OOOOh i got where i was wrong i didnt put that ',' being addicted to dreamweaver it didnt highlighted it :P wtv Thanks :) – kritya Jun 13 '11 at 13:14
9

You could probably write your own custom implementation of click/dblclick to have it wait for an extra click. I don't see anything in the core jQuery functions that would help you achieve this.

Quote from .dblclick() at the jQuery site

It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable.

Mick Hansen
  • 2,685
  • 18
  • 14
5

Look at the following code

$("#clickMe").click(function (e) {
    var $this = $(this);
    if ($this.hasClass('clicked')){
        alert("Double click");
        //here is your code for double click
        return;
    }else{
         $this.addClass('clicked');
        //your code for single click
         setTimeout(function() { 
                 $this.removeClass('clicked'); },500);
    }//end of else
});

Demo goes here http://jsfiddle.net/cB484/

Muhammad Tahir
  • 2,351
  • 29
  • 25
4

I've written a jQuery plugin that allow also to delegate the click and dblclick events

// jQuery plugin to bind both single and double click to objects
// parameter 'delegateSelector' is optional and allow to delegate the events
// parameter 'dblclickWait' is optional default is 300
(function($) {
$.fn.multipleClicks = function(delegateSelector, clickFun, dblclickFun, dblclickWait) {
    var obj;
    if (typeof(delegateSelector)==='function' && typeof(clickFun)==='function') {
        dblclickWait = dblclickFun; dblclickFun = clickFun; clickFun = delegateSelector; delegateSelector = null; // If 'delegateSelector' is missing reorder arguments
    } else if (!(typeof(delegateSelector)==='string' && typeof(clickFun)==='function' && typeof(dblclickFun)==='function')) {
        return false;
    }
    return $(this).each(function() {
        $(this).on('click', delegateSelector, function(event) {
            var self = this;
            clicks = ($(self).data('clicks') || 0)+1;
            $(self).data('clicks', clicks);
            if (clicks == 1) {
                setTimeout(function(){
                    if ($(self).data('clicks') == 1) {
                        clickFun.call(self, event); // Single click action
                    } else {
                        dblclickFun.call(self, event); // Double click action
                    }
                    $(self).data('clicks', 0);
                }, dblclickWait || 300);
            }
        });
    });
};
})(jQuery);
  • This plugin is great! But somehow it doesn't seem to work in IE9? I don't care for older versions (and you shouldn't anymore nowadays IMO) but I try to achieve functionality at least on IE9 - this is the browser anyone can have (no OS limitations) and which has good JS support. – Dennis98 Jul 04 '15 at 14:37
  • Usage: `$("body").multipleClicks('#mySelector', function(){ /* do something on click */},function(){/* do something on doubleclick */},300);` – Hafenkranich Oct 20 '16 at 22:56
  • This one actually enables you to *use* both – click and doubleclick– at the same time for different actions. Thank you! That one works like a charm. – Hafenkranich Oct 20 '16 at 22:57
3

i am implementing this simple solution , http://jsfiddle.net/533135/VHkLR/5/
html code

<p>Click on this paragraph.</p>
<b> </b>

script code

var dbclick=false;    
$("p").click(function(){
setTimeout(function(){
if(dbclick ==false){
$("b").html("clicked")
}

},200)

}).dblclick(function(){
dbclick = true
$("b").html("dbclicked")
setTimeout(function(){
dbclick = false


},300)
});


its not much laggy

3

This solution works for me

var DELAY = 250, clicks = 0, timer = null;

$(".fc-event").click(function(e) {
    if (timer == null) {
        timer = setTimeout(function() {
           clicks = 0;
            timer = null;
            // single click code
        }, DELAY);
    }

    if(clicks === 1) {
         clearTimeout(timer);
         timer = null;
         clicks = -1;
         // double click code
    }
    clicks++;
});
2
var singleClickTimer = 0; //define a var to hold timer event in parent scope
jqueryElem.click(function(e){ //using jquery click handler
    if (e.detail == 1) { //ensure this is the first click
        singleClickTimer = setTimeout(function(){ //create a timer
            alert('single'); //run your single click code
        },250); //250 or 1/4th second is about right
    }
});

jqueryElem.dblclick(function(e){ //using jquery dblclick handler
    clearTimeout(singleClickTimer); //cancel the single click
    alert('double'); //run your double click code
});
Gregory Ray
  • 305
  • 2
  • 4
1

I made some changes to the above answers here which still works great: http://jsfiddle.net/arondraper/R8cDR/

azzy81
  • 2,261
  • 2
  • 26
  • 37
1

Below is my simple approach to the issue.

JQuery function:

jQuery.fn.trackClicks = function () {
    if ($(this).attr("data-clicks") === undefined) $(this).attr("data-clicks", 0);

    var timer;
    $(this).click(function () {
        $(this).attr("data-clicks", parseInt($(this).attr("data-clicks")) + 1);

        if (timer) clearTimeout(timer);

        var item = $(this);
        timer = setTimeout(function() {
            item.attr("data-clicks", 0);
        }, 1000);
    });
}

Implementation:

$(function () {
    $("a").trackClicks();

    $("a").click(function () {
        if ($(this).attr("data-clicks") === "2") {
            // Double clicked
        }
    });
});

Inspect the clicked element in Firefox/Chrome to see data-clicks go up and down as you click, adjust time (1000) to suit.

mhapps
  • 1,023
  • 8
  • 15
0
(function($){

$.click2 = function (elm, o){
    this.ao = o;
    var DELAY = 700, clicks = 0;
    var timer = null;
    var self = this;

    $(elm).on('click', function(e){
        clicks++;
        if(clicks === 1){
            timer = setTimeout(function(){
                self.ao.click(e);
            }, DELAY);
        } else {
            clearTimeout(timer);
            self.ao.dblclick(e);
        }
    }).on('dblclick', function(e){
        e.preventDefault();
    });

};

$.click2.defaults = { click: function(e){}, dblclick: function(e){} };

$.fn.click2 = function(o){
    o = $.extend({},$.click2.defaults, o);
    this.each(function(){ new $.click2(this, o); });
    return this;
};

})(jQuery);

And finally we use as.

$("a").click2({
    click : function(e){
        var cid = $(this).data('cid');
        console.log("Click : "+cid);
    },
    dblclick : function(e){
        var cid = $(this).data('cid');
        console.log("Double Click : "+cid);
    }
});
Sergio FG
  • 9
  • 2
0

Same as the above answer but allows for triple click. (Delay 500) http://jsfiddle.net/luenwarneke/rV78Y/1/

    var DELAY = 500,
    clicks = 0,
    timer = null;

$(document).ready(function() {
    $("a")
    .on("click", function(e){
        clicks++;  //count clicks
        timer = setTimeout(function() {
        if(clicks === 1) {
           alert('Single Click'); //perform single-click action
        } else if(clicks === 2) {
           alert('Double Click'); //perform single-click action
        } else if(clicks >= 3) {
           alert('Triple Click'); //perform Triple-click action
        }
         clearTimeout(timer);
         clicks = 0;  //after action performed, reset counter
       }, DELAY);
    })
    .on("dblclick", function(e){
        e.preventDefault();  //cancel system double-click event
    });
});
Luen
  • 3
  • 5
0

This is a method you can do using the basic JavaScript, which is works for me:

var v_Result;
function OneClick() {
    v_Result = false;
    window.setTimeout(OneClick_Nei, 500)
    function OneClick_Nei() {
        if (v_Result != false) return;
        alert("single click");
    }
}
function TwoClick() {
    v_Result = true;
    alert("double click");
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

If you don't want to create separate variables to manage the state, you can check this answer https://stackoverflow.com/a/65620562/4437468

Utsav Barnwal
  • 985
  • 11
  • 14