Simulate click at x/y coordinates using javascript

Ahmed Hafez picture Ahmed Hafez · Apr 24, 2012 · Viewed 22k times · Source

How can I simulate a click at x/y co-ordinates using javascript or jquery?

I will use the script multiple times and I want the script to click on postion one then postion two then three then four and so one.

It is better without moving the mouse cursor, but if it has to move then that is fine as well.

Answer

Prestaul picture Prestaul · Apr 24, 2012

This can actually be accomplished with the document.elementFromPoint method. A jQuery example:

function simulateClick(x, y) {
    jQuery(document.elementFromPoint(x, y)).click();
}
simulateClick(100, 250);
simulateClick(400, 250);

Edit: Here is a working example: http://jsfiddle.net/z5YjY/