Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simulate pointerdown Event in JS

Tags:

javascript

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?

like image 649
Shayan Avatar asked Jun 13 '26 21:06

Shayan


1 Answers

MDN spec for creating & triggering events:

// 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.

like image 162
vsync Avatar answered Jun 15 '26 16:06

vsync



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!