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?

6
  • 1
    dblclick? Commented Jun 15, 2015 at 12:26
  • Why don't you do a google search before posting a question on Stackoverflow? Commented Jun 15, 2015 at 12:28
  • @bhspencer I did and I didn't find an answer that worked for me Commented Jun 15, 2015 at 12:30
  • really the MDN reference didn't work for you developer.mozilla.org/en-US/docs/Web/Events/dblclick or perhaps all the other SO questions on this topic stackoverflow.com/questions/5497073/… Commented Jun 15, 2015 at 12:31
  • @bhspencer why you so aggressive :) Commented Jun 15, 2015 at 12:37

2 Answers 2

3

Try like this

var event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true  });
document.getElementById("foo").dispatchEvent(event);
Sign up to request clarification or add additional context in comments.

Comments

0
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); 

5 Comments

I get this error "Uncaught TypeError: document.getElementById(...).dblclick is not a function"
what browser are you using? dblclick is not available on all browsers?
@bhspencer Google Chrome
try this: var targLink = document.getElementById ("something"); var clickEvent = document.createEvent ('MouseEvents'); clickEvent.initEvent ('dblclick', true, true); targLink.dispatchEvent (clickEvent);
@Identity1 that worked thank you, edit you answer with the new code to help other pepole

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.