Simulate click on x / y coordinates using javascript

How can I simulate a click on x / y coordinates using javascript or jquery?

I will use the script several times, and I want the script to click on postion, and then place two, then three, then four, etc.

It’s better not to move the mouse cursor, but if it should move, that’s good too.

+5
source share
2 answers

This can be done using the method document.elementFromPoint. 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/

+17
source

In the second step, I shared my impressions and updated Prestaul's answer.

, onclick, - (: ).

vanilla js, $.

function simulateClick(x, y) {
    var clickEvent= document.createEvent('MouseEvents');
    clickEvent.initMouseEvent(
    'click', true, true, window, 0,
    0, 0, x, y, false, false,
    false, false, 0, null
    );
    document.elementFromPoint(x, y).dispatchEvent(clickEvent);
}

X/pageY .

, : http://jsfiddle.net/V4CdC/

+5

All Articles