Search code examples
javascriptjqueryhtmlmouseeventmouseclick-event

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


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


Solution

  • 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();