0

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?

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

2 Answers2

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