最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Jul 11, 2011 at 9:44 kxhitiz asked Jul 11, 2011 at 9:38 kxhitizkxhitiz 1,9494 gold badges19 silver badges26 bronze badges 4
  • The pseudo-code you have here would result in three clicks - the original user click and then two triggered clicks. However, it may be more important to ask what you want to do with these double clicks. I'm assuming that some other app is listening for double clicks? If that is the case, a double-click is usually defined as having some interval between the two clicks so you need to take that into account. – T Nguyen Commented Jul 11, 2011 at 9:51
  • It depends on whether the actual number of clicks is important or you merely want the 'double-click' event to be triggered. – vinod Commented Jul 11, 2011 at 9:53
  • Sometimes, revising our business is a more intelligent decision. I think you'd better tell us your requirements, so that we might suggest better implementation methods. Otherwise follow @vinod answer. – Saeed Neamati Commented Jul 11, 2011 at 9:55
  • Basically my requirement is that I have a text box and there can be any length of words and to select each word, I have to double click each word. Doing single click just puts the cursor in it. So I wanted to trigger double click event whenever single click is done so that word gets selected where the cursor is pointed even in single click. Thanks! – kxhitiz Commented Jul 11, 2011 at 11:05
Add a ment  | 

1 Answer 1

Reset to default 1

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论