Reputation: 4778
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?
Upvotes: 0
Views: 759
Reputation: 4778
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 .
Upvotes: 0
Reputation: 15213
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 });
Upvotes: 1