Skip to content

Instantly share code, notes, and snippets.

@robotdana
Created May 4, 2011 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotdana/954764 to your computer and use it in GitHub Desktop.
Save robotdana/954764 to your computer and use it in GitHub Desktop.
// watches a text input/area and triggers a change event as soon as it changes. Doesn't wait for blur.
// Doesn't loop unless a relevant element is focused
(function($){
$.fn.extend({
val_change: function(callback){
var interval;
var $this = $(this);
$this.focus(function(){
val last_val, current_val;
interval = setInterval(function(){
var current_val = $this.val();
if (last_val !== current_val) {
$this.triggerHandler('val_change');
last_val = current_val;
}
}, 16);
}).blur(function(){
$this.unbind('val_change');
clearInterval(interval);
}).bind('val_change', callback);
return $this;
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment