0

Yo,

For the past weeks i try to simulate a double on my element but i can't find a solution. I know the click() method exist but i can't find a smilary method for my problem. Thanks for your help

Artis84
  • 3
  • 3

1 Answers1

1

You can trigger dblclick event by using dispatchEvent like this:

const event = new MouseEvent('dblclick');
document.getElementById('myId').dispatchEvent(event);

If you are using JQuery, The second way is:

$('#myId').dblclick()

var button = document.getElementById("myButton");
button.addEventListener("dblclick", function() {
  alert("Works!");
});

const event = new MouseEvent('dblclick');
button.dispatchEvent(event);
<button id="myButton">Test</button>
mikenlanggio
  • 1,122
  • 1
  • 7
  • 27
  • 1
    @Ivar I just updated my answer, It just works when using `JQuery` – mikenlanggio Dec 06 '22 at 09:02
  • 1
    Thanks. Do note that questions like these are very likely to have already been answered on Stack Overflow and [we prefer to close them as duplicates](https://stackoverflow.com/help/duplicates) instead of answering them. – Ivar Dec 06 '22 at 09:04