0

I would like to trigger the MouseEvent manually in typescript and it shows the initEvent is deprecated.

var clickEvent =document.createEvent('MouseEvent');
clickEvent.initEvent('mouseup',true,true);

in this screenshot, it shows the initEvent is deprecated.

Is there any alternate way to achieve this?

1 Answers1

1

initEvent is now depreciated as shown in the hint. A note in the docs says:

Note: Do not use this method anymore as it is deprecated. Instead use specific event constructors, like Event().

MouseEvent is an interface and it is used by different types of events. One of them being mouseup. The new way of doing this is :

const mouseupEvent = new MouseEvent("mouseup",{bubbles : true, cancelable: true});

// Dispatch the event.
document.dispatchEvent(clickEvent);
Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • There is a multi-select dropdown. I want to click the first value. Should I use "mousedown" instead of "mouseup"? like this ```const mouseupEvent = new MouseEvent("mousedown",{bubbles : true, cancelable: true}); ``` – Sriram Sundar Jun 16 '22 at 06:44
  • Mousedown works. There are subtle differences though. https://stackoverflow.com/questions/12572644/differentiate-click-vs-mousedown-mouseup#:~:text=onMouseDown%20will%20trigger%20even%20when,released%20on%20the%20same%20object. – Tushar Shahi Jun 16 '22 at 12:30
  • Do mark the answer as accepted/upvoted if it helps you. – Tushar Shahi Jun 16 '22 at 12:31