Trying to fire off (trigger) a click event. Its easy to do in jQuery, but cannot figure out how to set the coordinates of the event and send them along.
Essentially, I need to trigger a click at a specific location (which is calculated prior to the trigger() call).
Any way to do this (in jQuery or otherwise)?
Thanks -
The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position. The x position of the event is found using the 'clientX' property. The x position of the canvas element, i.e. the left side of the rectangle can be found using the 'left' property.
The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.
If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.
Set the pageX
and pageY
properties (which are normalized) on the event object and pass it to .trigger()
, like this:
var e = new jQuery.Event("click"); e.pageX = 10; e.pageY = 10; $("#elem").trigger(e);
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