0

To call a click event on an element element.click()

But how to do it with dblclick?

Is there an existing built-in function in javascript similar to element.click()?

Jo E.
  • 7,822
  • 14
  • 58
  • 94
  • Downvoted as the first hit on a google search for `javascript double click event` gives you the syntax. – adamdc78 Jan 14 '15 at 03:21
  • @adamdc78 I dont want to catch the double click event listener (e.g. `element.addEventListener("dblclick",function(e){console.log(e)},false);` ). I want to call it. and the results on google does not answer my question. – Jo E. Jan 14 '15 at 03:24
  • 1
    Also from the first hit on a google search: [How to programmatically fire a dblclick event defined with addEventListener?](http://stackoverflow.com/questions/18399215/how-to-programmatically-fire-a-dblclick-event-defined-with-addeventlistener) – adamdc78 Jan 14 '15 at 03:26
  • @adamdc78 that result didn't come out for me, but its helpful thanks! – Jo E. Jan 14 '15 at 03:34

2 Answers2

3

Just use ondblclick:

http://jsfiddle.net/dirtyd77/yrL8r6qd/

var p = document.getElementById('dbl');
p.ondblclick = function (){
  this.innerHTML = 'dbl';  
};
p.ondblclick(); // if you want to "trigger" the event, call it like so

When using addEventListener, you need to use dispatchEvent:

http://jsfiddle.net/dirtyd77/yrL8r6qd/2/

var p = document.getElementById('dbl');

p.addEventListener('dblclick',function(e){
    this.innerHTML = 'dbl';
});

p.dispatchEvent(new Event('dblclick'));
Dom
  • 38,906
  • 12
  • 52
  • 81
  • What if my code used `element.addEventListener("dblclick",function(e){console.log(e)},false);` how do I do it? `ondblclick()` is not working for me. – Jo E. Jan 14 '15 at 03:26
  • 1
    @Deadpool updated my answer, let me know if you need any help! Also this can help you out some, definitely worth a look: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events – Dom Jan 14 '15 at 03:34
1
object.ondblclick=function(){myScript};

or

object.addEventListener("dblclick", myScript);