Skip to content
Advertisement

How to simulate a mouse click using JavaScript?

I know about the document.form.button.click() method. However, I’d like to know how to simulate the onclick event.

I found this code somewhere here on Stack Overflow, but I don’t know how to use it 🙁

JavaScript

How do I fire a mouse click event using JavaScript?

Advertisement

Answer

(Modified version to make it work without prototype.js)

JavaScript

You can use it like this:

JavaScript

Note that as a third parameter you can pass in ‘options’. The options you don’t specify are taken from the defaultOptions (see bottom of the script). So if you for example want to specify mouse coordinates you can do something like:

JavaScript

You can use a similar approach to override other default options.

Credits should go to kangax. Here‘s the original source (prototype.js specific).

Advertisement