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.
This is the fastest& clean way to do that :
I'm using Jquery-->
It is working pretty fine for me.
I may be late to the party here but can you not just use the .change() event that jQuery provides.
You should be able to do something like ...
You could always bind it to a list of controls with something like ...
The live binder ensures that all elements that exist on the page now and in the future are handled.
We actually don't need to setup loops for detecting javaScript changes. We already setting up many event listeners to the element we want to detect. just triggering any un harmful event will make the job.
and ofc this is only available if you have the full control on javascript changes on your project.
Well, best way is to cover those three bases you listed by yourself. A simple :onblur, :onkeyup, etc won't work for what you want, so just combine them.
KeyUp should cover the first two, and if Javascript is modifying the input box, well I sure hope it's your own javascript, so just add a callback in the function that modifies it.
Can't you just use
<span contenteditable="true" spellcheck="false">
element in place of<input type="text">
?<span>
(withcontenteditable="true" spellcheck="false"
as attributes) distincts by<input>
mainly because:<input>
.value
property, but the text is rendered asinnerText
and makes part of its inner body.<input>
isn't although you set the attributemultiline="true"
.To accomplish the appearance you can, of course, style it in CSS, whereas writing the value as
innerText
you can get for it an event:Here's a fiddle.
Unfortunately there's something that doesn't actually work in IE and Edge, which I'm unable to find.
Unfortunately, I think
setInterval
wins 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
input
can get a value.The downsides of using 'setInterval' don't seem to apply in this case: