3

How can I trigger mouse left button click by specifying X and Y pixels offset from upper left website corner via JavaScript?

2
  • I have no clicks, but I have to issue/trigger a click. So subscribing to any events is not a solution. Commented Apr 1, 2014 at 13:00
  • Found the solution stackoverflow.com/questions/3277369/… Commented Apr 1, 2014 at 13:10

5 Answers 5

3

Well, using just Javascript as you asked, you can use mouse events, as you can read X and Y properties to get coordinaties from the event object, for sample:

// mouse move
document.body.onmousemove = function(e) {
   var x = e.X;
   var y = e.Y;
   console.log(e);
}

// mouse down
document.body.onmousedown = function(e) {
   var x = e.X;
   var y = e.Y;
   console.log(e);
}

To simulate an mouse click, you can call the event onmousedown manually, but you have to provide the event parameter (passing the coordinates, etc..), for sample:

document.body.onmousedown({X:120, Y:120 /* other properties */});
Sign up to request clarification or add additional context in comments.

2 Comments

I have no clicks, but I have to issue/trigger a click. So subscribing to any events is not a solution.
You can force the call of these events and use as a click on window, look my edits.
1

bind onclick event on document body, Try this code

document.body.onclick = function(e){
   if(e.clientX < 100 && e.clientY < 100){
       // your code here
   }
   alert("X =" + e.clientX +" Y"+ e.clientY)
};

2 Comments

I have no clicks, but I have to issue/trigger a click. So subscribing to any events is not a solution.
@IvanDoroshenko i'm not sure what are you mean exactly, this code is bide whole body in click event, you can only get specified x,y values in if condition this is also fake trigger click
0

Use jquery mousemove to get the x y coords when moving and then fire the click within it if necessary. Something like:

$("body").mousemove(function( event ) {
    if(event.pageX == 50 && event.pageY == 50)
    {
        $("targetclickelement").click();
    }
});

Would be pretty inefficient though...

Edit - without jquery, use the same principle but with the handlers in Felipe Oriani's answer.

1 Comment

OP want to solution in JavaScript.
0

I don't believe you can do such a thing, you could however catch the click then check the offsets and call function if it is within range

$('#d').click(function(event){
    if(event.offsetX > 50 && event.offsetX < 100 && event.offsetY > 100 && event.offsetY < 200) 
    {
        ///  Execute function
    }
});

1 Comment

I have no clicks, but I have to issue/trigger a click. So subscribing to any events is not a solution.
0

You can fire an element's click event, and you can get an element using x/y co-ordinates - so you could fire a click event on the element at x/y. Since you tagged jQuery:

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

https://developer.mozilla.org/En/DOM:document.elementFromPoint

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.