There are many ways the value of a <input type="text"> can change, including:
- keypresses
- copy/paste
- modified with JavaScript
- auto-completed by browser or a toolbar
- form reset
I want my JavaScript function to be called (with the current input value) any time it changes. And I want it to be called right away, not just when the input loses focus.
I’m looking for the cleanest and most robust way to do this across all browsers (using jQuery preferably).
Unfortunately, I think
setIntervalwins the prize:It’s the cleanest solution, at only 1 line of code. It’s also the most robust, since you don’t have to worry about all the different events/ways an
inputcan get a value.The downsides of using ‘setInterval’ don’t seem to apply in this case: