0

I'm working on a chrome extension and I'm curious how to double click DOM

When I want to just 1 click I use this function

document.getElementById('foo').click();

But some elements on a website needs a double click to be triggered

So Is there any method to do that with javascript?

Jim
  • 1,430
  • 5
  • 18
  • 41
  • 1
    [dblclick](https://developer.mozilla.org/en-US/docs/Web/Events/dblclick)? – CodingIntrigue Jun 15 '15 at 12:26
  • Why don't you do a google search before posting a question on Stackoverflow? – bhspencer Jun 15 '15 at 12:28
  • @bhspencer I did and I didn't find an answer that worked for me – Jim Jun 15 '15 at 12:30
  • really the MDN reference didn't work for you https://developer.mozilla.org/en-US/docs/Web/Events/dblclick or perhaps all the other SO questions on this topic http://stackoverflow.com/questions/5497073/how-to-differentiate-single-click-event-and-double-click-event – bhspencer Jun 15 '15 at 12:31
  • @bhspencer why you so aggressive :) – Jim Jun 15 '15 at 12:37
  • @Jim because SO is increasing full of very low quality and duplicate questions. The very least a user should do when posting a question is search SO to see if it has been asked before. – bhspencer Jun 15 '15 at 13:11

2 Answers2

3

Try like this

var event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true  });
document.getElementById("foo").dispatchEvent(event);
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
1
document.getElementById('foo').dblclick();

check .dblcick()

If that doesn't work then use dispatchEvent() to simulate something similar.

 var target = document.getElementById('foo');
 var dblClickEvent= document.createEvent('MouseEvents'); 
 dblClickEvent.initEvent ('dblclick', true, true); 
 targLink.dispatchEvent (dblClickEvent); 
Identity1
  • 1,139
  • 16
  • 33
  • I get this error "Uncaught TypeError: document.getElementById(...).dblclick is not a function" – Jim Jun 15 '15 at 12:29
  • what browser are you using? dblclick is not available on all browsers? – bhspencer Jun 15 '15 at 12:30
  • @bhspencer Google Chrome – Jim Jun 15 '15 at 12:31
  • try this: var targLink = document.getElementById ("something"); var clickEvent = document.createEvent ('MouseEvents'); clickEvent.initEvent ('dblclick', true, true); targLink.dispatchEvent (clickEvent); – Identity1 Jun 15 '15 at 12:31
  • @Identity1 that worked thank you, edit you answer with the new code to help other pepole – Jim Jun 15 '15 at 12:35