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!

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. 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. 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. 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! Commented Jul 11, 2011 at 11:05

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.