How to simulate a click by using x,y coordinates in JavaScript?

RadiantHex picture RadiantHex · Jul 18, 2010 · Viewed 124.7k times · Source

Is it possible to use given coordinates in order to simulate a click in JavaScript within a webpage?

Answer

Andy E picture Andy E · Jul 18, 2010

You can dispatch a click event, though this is not the same as a real click. For instance, it can't be used to trick a cross-domain iframe document into thinking it was clicked.

All modern browsers support document.elementFromPoint and HTMLElement.prototype.click(), since at least IE 6, Firefox 5, any version of Chrome and probably any version of Safari you're likely to care about. It will even follow links and submit forms:

document.elementFromPoint(x, y).click();

https://developer.mozilla.org/En/DOM:document.elementFromPoint https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click