How to specify the coordinate parameters when simulating mouse events in javascript

We have known the meaning and difference of the coordinate parameters in a java script event object. To simulate mouse event with javascript,you need to create a mouse event then dispatch it to the element.

evt = document.createEvent("MouseEvents");
evt.initMouseEvent(type,
bubbles, cancelable, view, detail,
screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey,
button, document.body.parentNode);

var element = document.getElementById('myelement');

element.dispatchEvent(evt);

You may wonder how to specify the coordinates in the initMouseEvent? If I do not know or specify the wrong coordinates of the element to click on, can the event be received and handled? In fact, you do not need to know the coordinates(specifying random values for the coordinate parameters) if you just want to fire the event handler. The event handling function is successfully called passing the event object you created as the parameter(although the coordinates are not real ones). You can even use the initEvent function instead of the initMouseEvent to initialize the event object where you need not specify the coordinates at all.

evt.initEvent( 'click', true, true );

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Comments are closed, but trackbacks and pingbacks are open.