I'm looking for a jquery/simple javascript (not some other library) solution that set an event to a textbox that will fire when the textbox being changed? (not after blur)
I need it to fire no matter how it was changed (keyboard char key/tab/enter or mouse paste) And I need it to be fired one time only.
-
Does the 'onchange' event not work for your textbox?Blazemonger– Blazemonger2011-11-02 13:56:57 +00:00Commented Nov 2, 2011 at 13:56
-
possible duplicate of <input> text change eventsTim Down– Tim Down2011-11-02 14:07:31 +00:00Commented Nov 2, 2011 at 14:07
6 Answers
Unfortunately, there isn't one. There will be some day, when all browsers support the textinput event and/or the HTML5 input event, but currently not all do, and those that do have various bugs. You can use a combination of events, but you have to handle responding to each change only once yourself, and there's always the possibility of failing to cover some browser somewhere.
So if you absolutely, positively have to do this, you have to poll the value property every N milliseconds. (Choose the largest number you're happy with, to avoid making the browser work too hard, and make sure the code in the timer function is as tight and quick as possible — e.g., don't re-look-up the element each time, look it up once and keep a reference to it.)
4 Comments
input event. stackoverflow.com/questions/5519936/jquery-keyboard-events/…. Works in everything except IE < 9, in which you can use the propertychange event instead.input event. At the moment, I think polling remains the only truly reliable mechanism. Hopefully that will change in the next couple of years.You can bind the keyup event.
$("textarea").bind("keyup", function() {
// Fires after the user has typed
});
1 Comment
var obj = document.getElementById("field");
var func= function(){
alert('hi');
}
obj.onkeypress= func;
obj.onclick = func;
obj.on... = func;
1 Comment
change isn't fired until the user exits the field.You can use jQuery keydown or keyup.
var target = jQuery('#id');
target.keydown( function () { alert('hi'); } );
Now, since you want to be called only once, all you have to do is to unbind right after it's called:
target.keydown( function () { alert('hi'); target.unbind('keydown'); } );