Is it possible to simulate left mouse click on the element that lies beneath the mouse cursor?
Asked
Active
Viewed 1,687 times
2 Answers
5
You can fire a click on the document:
$(document).click();
If you want the element under the cursor to be clicked then it's slightly more complicated:
var mousePosition;
$(window).on('mousemove', function (e) {
mousePosition = {
x: e.clientX,
y: e.clientY
}
});
function makeClick () {
var el = document.elementFromPoint(mousePosition.x, mousePosition.y);
$(el).click();
}
When you want a click under the cursor, call makeClick()
.

Jivings
- 22,834
- 6
- 60
- 101
-
@Paulius You didn't say anything in your question about wanting links to be clicked. – Jivings Oct 21 '14 at 13:24
-
in your case: $(document).click(); Where would the click done? I mean if a mouse cursor id on the link than it should be clicked or not? – Paulius Oct 21 '14 at 13:32
-
@Paulius Okay so you want whatever is under the cursor to be clicked? Because that's a different question :) – Jivings Oct 21 '14 at 13:34
-
,sorry for wrong question at first. Yes I would like whatever is under cursor to be clicked. – Paulius Oct 21 '14 at 13:36
-
Thank you but it is still not working. I put the code here: My code is here: http://demo.kitoks.com/paulius/test/test.php I also posted the code in answers below. – Paulius Oct 21 '14 at 20:33
-
@Paulius I see what the problem is, `click` events don't work on links like that. It is actually clicking the link, it just wont follow it. If you wanted to redirect you'd have to do `window.location = $(el).attr('href')` – Jivings Oct 22 '14 at 08:41
-1
To simulate a click with jQuery it woild be:
$("item").click();
In Javascript:
document.getElementById('item').click();

Joe Kasavage
- 503
- 5
- 13
-
1I don't need a click on a certain element. But just simple mouse click (anywhere where the cursor is). – Paulius Oct 21 '14 at 13:21