How do I double-click on objects using javascript? Do I have to .click() twice?

user3365609 picture user3365609 · May 29, 2014 · Viewed 21.9k times · Source

I tried searching for this but the only results that come up are on double click, and never how to automatically double click on something. (trigger a double-click.)

I can click once, and have tried doing so twice to "Create" a double-click but failed. I'm assuming that it's because of the timing which is why I set up a timer to see how much time there is between my double clicks.

Is there an automated way to double click? I'm not using jQuery so please avoid it in the answer.

So I click using:

document.getElementyById("somethinbg").click();

I tried double clicking with:

document.getElementById("something").dblclick(); 

With no success.

Answer

Brock Adams picture Brock Adams · May 29, 2014

Dispatch a dblclick event like so:

var targLink    = document.getElementById ("something");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('dblclick', true, true);
targLink.dispatchEvent (clickEvent);


You can see the code in action at jsFiddle.