I've created a jQuery selector called jquery.observe_field. It mimics Prototype's observe_field. My plugin works ok with "single" selectors, like an ID selector:
$('#foo').observe_field(1, function() { alert(this.value); });
But it doesn't seem to work (I don't get any errors, but alerts are just never shown) when a multiple selector is used:
$('#foo,#bar,#baz').observe_field(1, function() { alert(this.value); });
Why is that?
EDIT: I've created an example in jsFiddle (notice that in the JavaScript part, the initialization code is right after the plugin code): http://jsfiddle.net/X2Bzk/
(function($) {
jQuery.fn.observe_field = function(frequency, callback) {
return this.each(function() {
var $this = $(this);
var prev = $this.val();
var check = function() {
var val = $this.val();
if (prev != val) {
prev = val;
$this.map(callback); // invokes the callback on $this
}
};
var reset = function() {
if (ti) {
clearInterval(ti);
ti = setInterval(check, frequency);
}
};
check();
frequency = frequency * 1000; // translate to milliseconds
var ti = setInterval(check, frequency); // invoke check periodically
// reset counter after user interaction
$this.bind('keyup mousemove', reset); //mousemove is for selects
});
};
})(jQuery);