Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering a JavaScript click() event at specific coordinates

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 -

like image 271
OneNerd Avatar asked May 16 '10 19:05

OneNerd


People also ask

How do you trigger an event on click?

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.

How do you find the coordinates of a click?

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.

What is Click () method?

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.

How do I trigger a click event without clicking?

If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.


1 Answers

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); 
like image 150
Nick Craver Avatar answered Sep 20 '22 21:09

Nick Craver