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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With