/*
* Polls an input element while it has focus.
*
* %("input#search").observe( function() {
* console.log("Your search: " + $(this).val() );
* });
*/
$.fn.observe = function(callb, interval) {
interval = interval === undefined ? 200 : interval;
this.each(function(i, obj) {
var self = $(obj);
var previousValue = self.val();
var hasFocus = false;
self.focus(function() {
hasFocus = true;
setTimeout(function(){
(previousValue !== self.val()) && callb.call(obj);
previousValue = self.val();
hasFocus && setTimeout(arguments.callee , interval);
}, interval);
self.bind( "blur" , function(){
self.unbind('blur', arguments.callee);
hasFocus = false;
} );
});
});
}
$("#message-input").observe( function(){
$("#message-output").text($(this).val());
});