is it possible to simulate/emulate pressing keyboard button or mouse button in jquery? I'm sure that it is possible, but how to handle it?
Asked
Active
Viewed 755 times
0
-
2Look at this: **[stackoverflow - Simulate Keypress With jQuery](http://stackoverflow.com/questions/1468384/simulate-keypress-with-jquery)** – f605dcf7dc5542e93ae9cd76f Aug 14 '13 at 12:43
-
http://stackoverflow.com/questions/13821071/jquery-script-for-simulated-key-press-down-not-running-keyboard-shortcut – Venugopal Aug 14 '13 at 12:45
2 Answers
1
You can do:
$('a').trigger('click'); // Mouse click
$(document).trigger('keydown'); // Keyboard
But you will need to add the events in order to be able to trigger them:
$('a').on('click',function() { // do something });
$('document').on('keydown',function() { // do something });

putvande
- 15,068
- 3
- 34
- 50
0
From official dos:
.trigger() Description: Execute all handlers and behaviors attached to the matched elements for the given event type.
Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user:
$('#foo').on('click', function() {
alert($(this).text());
});
$('#foo').trigger('click');
'click' can be replaced with an event from Keyboard Events or Mouse Events .

Alex Kulinkovich
- 4,408
- 15
- 46
- 50