简体   繁体   中英

Triggering double click via jQuery or pure Javascript for a single click event

I want to trigger double click event on any element when a single click event occurs in that element.

To be more clear, let's say I have a text box with some text, and when the user clicks(single click) on the text box I have to trigger that single click to multiple clicks(either double click or even tripple click).

I tried the following way, but in vain:(

$('#timer').click(function() {
  $('#timer').dblclick();
});

Thanks in advance.

Cheers!

The code you provided above works for me. A double click is triggered when a single click occurs. I used this variation:

var numd = 0; 
$("#content").dblclick(function() { 
    numd++; 
});
$("#content").click(function() { 
    $(this).dblclick(); 
});

numd is incremented correctly.

For multiple clicks:

You could use a variable to keep track of which click you are on while using the click() method to perform clicks. Here is an example to trigger a triple click.

var clicknum = 0;
$("#text-box").click(function() {
    clicknum++;
    if (clicknum < 3) {
        $(this).click();
    }
    else {
        // Reset clicknum since we're done.
        clicknum = 0;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM