Dan G. Switzer, II wrote:
Josh,
One more quick thing, is it possible to attach this to the val() of a
single input, or am I limited to extending this globally?
Instead of calling the below method "val" call it something else--like
"valChange()" or something. Then just call that new method any time you want
the change event to be triggered.
Also, since you're really only wanting to do this for a single field, you
could also just avoid the plug-in and just do:
$("#myField").val("new value").trigger("change");
That would trigger the "change" event when you update the value.
$.fn.extend({
val: function( val ) {
return val == undefined ?
( this.length ? this[0].value : null ) :
this.attr( "value", val ).trigger("change");
}
});
And if you'd want a plugin there's not really a need to overwrite the
existing method which a. duplicates code and b. can cause trouble
somewhere else (I for example experienced some trouble recently because
interface overwrites jQuery's own animate method which behaved
differently then):
$.fn.extend({
valChange: function(v) {
return this.val(v).trigger('change');
}
});
--Klaus