6

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.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
Ahmed Hafez
  • 115
  • 1
  • 1
  • 5
  • 4
    `try {writing some code} catch(can't do it) {throw(tantrum);}` – Niet the Dark Absol Apr 24 '12 at 20:31
  • 1
    I would like to hear more about what you are trying to accomplish... Call me a skeptic, but I am suspicious that you may be trying to cheat your way into some click-based income. – Prestaul Apr 24 '12 at 20:40
  • possible duplicate of [Simulate a click by using x,y coordinates? - Javascript](http://stackoverflow.com/questions/3277369/simulate-a-click-by-using-x-y-coordinates-javascript) – Prestaul Apr 24 '12 at 20:53
  • @Prestaul I wouldn't worry about it, if that's his goal he's going to be sorely disappointed – Dagg Nabbit Apr 24 '12 at 20:57
  • i dont understand why my question changed i didnt write it like that – Ahmed Hafez Apr 24 '12 at 21:16
  • @AhmedHafez, because Prestaul edited your question to, im his words *Fixed grammer/formatting, removed insultingly silly requests for good answers from "professionals"*, in order to help your question get better answers. – David Thomas Apr 24 '12 at 21:44
  • @AhmedHafez, I did [edit your post](http://stackoverflow.com/faq#editing)... I did it in hopes that it would help you to get better answers, but if you feel that it no longer asks for what you need then please provide more details. We are all working to get you the best answer that we can. – Prestaul Apr 25 '12 at 14:18
  • ok no proplem @Prestaul but why there are giving me - negatives what ive done to them ? – Ahmed Hafez Apr 25 '12 at 19:30
  • You've done nothing to them. They are simply voting down your answer because your capitalization, punctuation and grammer were not correct and the actual question was hidden by other comments. The downvotes simply indicate that people did not believe that your question would be helpful to others. – Prestaul Apr 25 '12 at 20:36
  • they blocked me from making any other questions this is totally un fair totally unfair – Ahmed Hafez May 02 '12 at 05:00
  • Possible duplicate of [How to simulate a click by using x,y coordinates in JavaScript?](https://stackoverflow.com/questions/3277369/how-to-simulate-a-click-by-using-x-y-coordinates-in-javascript) – Keith Jan 03 '18 at 16:11

2 Answers2

20

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/

Prestaul
  • 83,552
  • 10
  • 84
  • 84
  • why i see -2 this is realy strange any way thank u for ur reply i will try it and tell u – Ahmed Hafez Apr 24 '12 at 21:12
  • im sorry but how can i use this code can u please give me the full code to put before it – Ahmed Hafez Apr 24 '12 at 21:45
  • 1
    @AhmedHafez, put what I wrote inside of a ` – Prestaul Apr 25 '12 at 14:15
  • @AhmedHafez, to get jQuery on the page you can use `` – Prestaul Apr 25 '12 at 14:20
  • 1
    @AhmedHafez, I added a working example that simulates 5 clicks and reports the text in the element at that position. Take a look and see if that helps. – Prestaul Apr 25 '12 at 14:28
  • @Prestaul thank you thank you thank you thank you thank you big hugggggggggggggggggg great reply :D how can i help u too ? – Ahmed Hafez Apr 25 '12 at 19:00
  • yes i tried it it is realy nice and yes i want it for iframe actually :) i was tring yesterday all the day it didnt work on iframe i dont know why exactly it is supposed to work as it clicks and it doesnt matter what it clicks under the cursor or the hidden cursor but ur right it is better to make another question about it but for this question just one thing can u make it like rather than redirect the page to pop up a new window and also what about the timing factor WE want to make this question to help anyone making anything he could imagine :) – Ahmed Hafez Apr 25 '12 at 19:27
  • 1
    Yeah, this won't work with an iframe and if you have other questions about iframes, timing, and such then I would post them as new questions, but pay careful attention to your English or you may get downvoted before you get good answers. It might be worthwhile to have a coworker or friend review your questions for you. – Prestaul Apr 25 '12 at 20:39
  • omg they downvote me because of My English this so unfair so i downvote them if they write in arabic it is like uuhh what is this English bfff negative uff they are like kids minds realy – Ahmed Hafez Apr 26 '12 at 00:03
  • any way thank u Prestaul i will make other questions as u wish and may be hire some one to check my spelling because i think there may be some proplems with e and E with this type of users – Ahmed Hafez Apr 26 '12 at 00:04
  • Not working with . I tried with your code at page: http://codepen.io/r0ysy0301/full/grjaOp/. – Ave Jul 29 '16 at 05:53
  • @VănLộc, I don't see any click simulations in that code. What problem were you seeing with canvas? – Prestaul Aug 17 '16 at 17:31
5

On a second take, I'd share my experiences, and update Prestaul's answer.

In most cases, you don't just want to click on an element to induce the 'onclick' event, but you would like to use the click info for something (eg.: move a slider there, or put a popup at that exact place).

I'm mostly using vanilla js, but it blends well with $.

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);
}

Basically this will pass down the correct pageX / pageY through the event, for further use.

I have updated the fiddle as well, to show a use of these coords: http://jsfiddle.net/V4CdC/

Alex
  • 259
  • 3
  • 7