10

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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295

4 Answers4

34

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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks Brock! Is the command I posted not defined in javascript? –  May 29 '14 at 06:58
  • No it isn't. There is a `dblclick` event but no corresponding shortcut function. PS: you should never try to use those shortcuts, like `.click()` from a Tampermonkey script anyway -- due to scope and sandbox issues. – Brock Adams May 29 '14 at 07:05
  • Could you expand on that and give a different solution? –  May 29 '14 at 10:44
  • How so? Clarify. Ask a new question if you need to. – Brock Adams May 30 '14 at 03:42
  • I meant that I don't really understand what you mean by sandbox issues, and what other way you'd recommend, I tried searching regarding sandbox without much success. Thanks for your answer regardless :) –  May 30 '14 at 04:10
  • See [here](http://stackoverflow.com/a/13485650/331508) and [here](http://stackoverflow.com/a/17378538/331508). For *triggering* an event use `createEvent`, `initEvent` plus `dispatchEvent` code like in the answer above. – Brock Adams May 30 '14 at 04:23
0

With jquery dblclick method you can that very easily. Some thing like.

$("p").dblclick(function(){
   alert("The paragraph was double-clicked.");
});

see following link for more information

http://www.w3schools.com/jquery/event_dblclick.asp

http://api.jquery.com/dblclick/

and If you are using javascript then see following link.

Javascript Double Click Event

Community
  • 1
  • 1
Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
  • 8
    The OP is asking how to ***trigger*** a double-click, not listen for one. JavaScript-only (no jQuery) was also specified. – Brock Adams May 29 '14 at 06:56
  • Thanks adams, maybe I didn't ask clearly, I'm looking to trigger it. –  May 29 '14 at 06:57
0

document.querySelector("#MyID").dispatchEvent(new MouseEvent("dblclick"))

Example

<h3>press <kbd>Ctrl</kbd></h3>
<p>double click here</p>

<script>
  const targetElem = document.querySelector(`p`)

  targetElem.ondblclick = ()=>console.log("double click")

  document.addEventListener("keydown", (keyboardEvent) => {
    if (keyboardEvent.key === "Control") {
      targetElem.dispatchEvent(new MouseEvent("dblclick"))
    }
  })
</script>

MouseEvent Inheritance:

so you can do init as below

new MouseEvent("dbclick", {
screenX: 0, screenY: 0 button: 0, //... //  MouseEvent itself
bubbles: true, cancelable: true, composed: false, //  Event
view: window //  UIEvent
// ... 
})

i.e. The MouseEventInit dictionary also accepts fields from UIEventInit and from EventInit dictionaries.


Note Event.initEvent() is deprecated. Use Event instead of it.

Carson
  • 6,105
  • 2
  • 37
  • 45
-1
document.getElementById("something").ondblclick = function(){
       alert("The paragraph was double-clicked.");
}
Jan Remunda
  • 7,840
  • 8
  • 51
  • 60
jhyap
  • 3,779
  • 6
  • 27
  • 47