Ad

How To Call A Keypress Event By Jquery Without Pressing Any Keys

- 1 answer

I would like to realize a keypress event for typing a string into an input elements by Jquery, I know Jquery can listen the event of keypress, keydown and keyup. But, what I want to do is using the Jquery to realize the action of keypress and put the value of which key I pressed into the input elements. Is that possible by jQuery to realize this task?

Ad

Answer

Is this what you want?

WORKING FIDDLE

$('.input').on('keypress', function(event) {
  event.preventDefault();
  $(this).val(event.keyCode);
});

$( ".input" ).trigger( "keypress" );
Ad
source: stackoverflow.com
Ad