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
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).
Example use case: On the Twitter Signup page, the username field's value gets shown in the url "http://twitter/username" below it.
Here's a working example that I'm using to implement an autocomplete variation the populates a jqueryui selector (list), but I don't want it to function exactly like the jqueryui autocomplete which does a drop-down menu.
Binding to the
oninput
event seems to work fine in most sane browsers. IE9 supports it too, but the implementation is buggy (the event is not fired when deleting characters).With jQuery version 1.7+ the
on
method is useful to bind to the event like this:Here is a slightly different solution if you didn't fancy any of the other answers:
Consider that you cannot rely on click and keyup to capture context menu paste.
Add this code somewhere, this will do the trick.
Found this solution at val() doesn't trigger change() in jQuery