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.

11
  • 4
    try {writing some code} catch(can't do it) {throw(tantrum);} Commented Apr 24, 2012 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. Commented Apr 24, 2012 at 20:40
  • possible duplicate of Simulate a click by using x,y coordinates? - Javascript Commented Apr 24, 2012 at 20:53
  • @Prestaul I wouldn't worry about it, if that's his goal he's going to be sorely disappointed Commented Apr 24, 2012 at 20:57
  • i dont understand why my question changed i didnt write it like that Commented Apr 24, 2012 at 21:16

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

12 Comments

why i see -2 this is realy strange any way thank u for ur reply i will try it and tell u
im sorry but how can i use this code can u please give me the full code to put before it <script type="text/javascript"> or what and will it make auto click on postion 100 25 and postion 400 250 in the page even if it was an iframe and can i make also right click what jquey do i have to include in the head
@AhmedHafez, put what I wrote inside of a <script> tag, include jquery, and it should work. I don't believe that there is a way to simulate a right-click, and the technique for simulating a click inside of an iframe is going to be very different. If you need to be able to do that then the iframe will have to be on the same domain as the parent window, and I would recommend creating a second question. (i.e. "How to simulate a click inside of an iframe")
@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.
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.
|
6

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.