This content originally appeared on David Walsh Blog and was authored by David Walsh
Coding HTML forms has been painful my entire career. Form controls look different between operating systems and browsers, coding client side and server side validation is a nightmare, and inevitably you forget something somewhere along the line. Some behaviors don’t act the way you’d hope, like onChange
, which only fires when the user leaves (blur
s) a given form controls. Enter the onInput
event, which changes upon keystroke, paste, etc.
// Try it here: https://codepen.io/darkwing/pen/KKmBNvg myInput.addEventListener('input', e => { console.log(e.target.value); });
These days it seems like the old onChange
behavior isn’t useful — we always want to react to any user input. onInput
also fires on elements with contenteditable
and designmode
attributes. Most modern JavaScript libraries like React treat onChange
like onInput
, so it’s as though onChange
has lost its use!
The post onInput Event appeared first on David Walsh Blog.
This content originally appeared on David Walsh Blog and was authored by David Walsh
David Walsh | Sciencx (2021-08-03T02:03:11+00:00) onInput Event. Retrieved from https://www.scien.cx/2021/08/03/oninput-event/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.