Skip to content

Instantly share code, notes, and snippets.

@castus
Created May 16, 2013 10:11
Show Gist options
  • Save castus/5590735 to your computer and use it in GitHub Desktop.
Save castus/5590735 to your computer and use it in GitHub Desktop.
[js] A better onChange event
var input = jQuery("textarea");
function logger() { console.log(Array.prototype.slice.call(arguments)); }
function okay(e) {
logger(e.keyCode, input.val());
}
function better(e) {
setTimeout(function() {
logger(e.keyCode, input.val());
}, 0);
}
// This only occurs when a key generates output
input.on("keypress", okay);
// Better, but some browsers don't implement it properly...
input.on("change", okay);
// This will give you the output instantly when any key is pressed
input.on("keydown", better);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment