I have a div that has pointerdown event listener and I would like to simulate pointer down event by script. How do I do that?
// create a specific "pointerdown" event
var myEvent = new PointerEvent('pointerdown')
// Listen for the event (POC)
document.body.addEventListener('pointerdown', console.log);
// Dispatch the event (on the "<body>" element)
document.body.dispatchEvent(myEvent );
As you can see I am using the PointerEvent constructor.
Different events have different constructors, and you need to look which one has a constructor, and if there's none, you should use CustomEvent constructor.
Probably 99% of any DOM-related questions are already answered on MDN docs, you only need to connect the pieces together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With