Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dispatch a double click event on an element?

I can dispatch a simple click event (without any optional parameters) with:

var clickEvt = document.createEvent("MouseEvents");
clickEvt.initEvent("click");
element.dispatchEvent(clickEvt);

But how do I dispatch a double-click event?

like image 650
gvlasov Avatar asked Jul 23 '12 16:07

gvlasov


2 Answers

Just substitute "click" with "dblclick" in clickEvt.initEvent:

var dblclickEvt = document.createEvent("MouseEvents");
dblclickEvt.initEvent("dblclick");
element.dispatchEvent(dblclickEvt);
like image 107
gvlasov Avatar answered Oct 11 '22 14:10

gvlasov


The feauture used in the accepted answer (Event.initEvent()) has been deprecated.

Instead, use specific event consructors like Event()

So to answer your question, it would be something like :

var doubleClickEvent = new Event('dblclick');      //define double click event

element.dispatchEvent(doubleClickEvent);           //trigger double click even
like image 25
Snedden27 Avatar answered Oct 11 '22 15:10

Snedden27