0
votes

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?

2
Do you want that jQuery fills in a textbox not actually with the pressed value key but with its keycode? - Eduardo Escobar
yes, this is what I want. - Calvin

2 Answers

1
votes

Is this what you want?

WORKING FIDDLE

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

$( ".input" ).trigger( "keypress" );
0
votes
// Target input element and bind to the "keyPress" event.
$('.input').on('keypress', function(event) {
  // Prevent the default action to stop the key character being entered into the field
  event.preventDefault();
  // Add the event's keyCode into the field
  $(this).val(event.keyCode);
})

http://codepen.io/anon/pen/XXNOQd?editors=101