Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript addEventListener : 'input' versus 'keyup' [duplicate]

Tags:

javascript

I use the 'keyup' event when I want to get the value of a text-input immediately after insertion.

Now I've seen in a CodeReview question the usage of an event 'input' for to accomplish the same.

The CodeReview question (5th line): https://codereview.stackexchange.com/questions/141937/registration-form-validation-in-jquery

I've tinkered a bit. Made this demo:

var box = document.querySelector('input');

box.addEventListener('keyup', function() {
  console.log('keyup: %s', this.value);
});

box.addEventListener('input', function() {
  console.log('input: %s', this.value);
});

/* RESULT
  input: a
  keyup: a
  input: ab
  keyup: ab
  input: abc
  keyup: abc
*/
<input type="text" />

As one can see: No difference!

Therefore my question:

Is there a difference between the usage of the keyup-event and the input-event (for reading the value of a text-input)?

And:

Are there cases in which one should prefer the one over the other?

like image 285
cluster1 Avatar asked Sep 27 '16 07:09

cluster1


1 Answers

Key up event also detects the control keys like enter,Ctrl,backspace,delete etc on the other hand input event in initiated only for characters numbers and symbols. This is the one difference i found,

Key-up listens all keys

input listens only the characters ,numbers and special characters keys

like image 181
Mukul Jayaprakash Avatar answered Oct 05 '22 13:10

Mukul Jayaprakash