Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger mouse click by window offset in javascript [duplicate]

Tags:

javascript

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

like image 229
Ivan Doroshenko Avatar asked Jun 21 '26 00:06

Ivan Doroshenko


1 Answers

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 */});
like image 87
Felipe Oriani Avatar answered Jun 23 '26 13:06

Felipe Oriani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!