At the moment, the only way to make sure that you immediately get all the changes in the contents of the input is to poll it. This is because there are many possible ways to edit input without creating an event keyupor immediate change. Besides cutting and pasting, drag and drop, undo / redo, auto-complete, spell check ...
HTML5 offers an event inputthat will fire in all cases, for example, but browser support does not yet exist, unfortunately.
You can, of course, back up the survey to quickly check the general case when you receive an event. For instance:
function watchInput(input, callback) {
var value= input.value;
function check() {
if (input.value!==value)
callback.call(input, value);
value= input.value;
}
input.onchange=input.onkeyup= check;
setInterval(check, 400);
};
watchInput(document.getElementById('mytextarea', function(oldvalue) {
document.getElementById('mybutton').className= this.value===''? 'buttonDisabled' : 'buttonEnabled';
});
source
share