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.

- 147,333
- 58
- 291
- 367
-
Does the 'onchange' event not work for your textbox? – Blazemonger Nov 02 '11 at 13:56
-
possible duplicate of [ text change events](http://stackoverflow.com/questions/6056819/input-text-change-events) – Tim Down Nov 02 '11 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.)

- 1,031,962
- 187
- 1,923
- 1,875
-
3Yes there is: the HTML5 `input` event. http://stackoverflow.com/questions/5519936/jquery-keyboard-events/5524996#5524996. Works in everything except IE < 9, in which you can use the `propertychange` event instead. – Tim Down Nov 02 '11 at 14:04
-
@TimDown: You should have posted that as an answer. :-) I knew I was forgetting something. But unfortunately, various browsers [have various bugs](http://whattheheadsaid.com/2011/10/update-html5-oninput-event-plugin-for-jquery) in their support of the `input` event. At the moment, I think polling remains the only truly reliable mechanism. Hopefully that will change in the next couple of years. – T.J. Crowder Nov 02 '11 at 14:48
-
SO (both the system and a moderator) has recently told me off for posting answers that are essentially just links to other answers, so I've stopped doing it. Regarding the issues, only the IE backspace/delete key one seems serious to me, but could be mitigated by a bit of key detection. – Tim Down Nov 02 '11 at 14:57
-
@TimDown: Ah, yes, that. Don't get me started. Heaven forfend they should let someone as well-informed, helpful, and intelligent as yourself make your *own* decisions about how to best help people. – T.J. Crowder Nov 02 '11 at 15:08
I used T.J. Crowders's answer and came up with this code:
(function() {
setInterval(function() {
var validated = false;
if ($('#mytextinput').val() !== '' && validated === false) {
// perform validation or whatever you need
validated = true;
}
else {
validated = false;
}
}, 500);
})();

- 2,907
- 1
- 25
- 16
You can bind the keyup event.
$("textarea").bind("keyup", function() {
// Fires after the user has typed
});

- 46,145
- 13
- 104
- 123
var obj = document.getElementById("field");
var func= function(){
alert('hi');
}
obj.onkeypress= func;
obj.onclick = func;
obj.on... = func;

- 943
- 1
- 8
- 17
-
4He said he wants it proactively, not on blur. `change` isn't fired until the user exits the field. – T.J. Crowder Nov 02 '11 at 14:00
Aside from the bind
method outlined in another answer, you can also do this:
$("#txtTest").keyup(function(){
alert($(this).val());
});

- 45,496
- 8
- 73
- 110
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'); } );

- 459
- 2
- 6
-
2
-
-
@gdoron, I am not an expert. [T.J. Crowder's answer](http://stackoverflow.com/questions/7981513/javascript-jquery-event-that-fires-when-textbox-changes/7981579#7981579) looks the best so far. – Darin Dimitrov Nov 03 '11 at 06:59
-
mouse paste from clipboard, hm, i guess you could use the .change() case for both mouse and keyboard, but i cannot remember if it fires up on blur or on actual change of the content – John Nov 04 '11 at 12:47
-
Also, someone could paste something from the browsers toolbar ( Edit > Paste ) so T.J. Crowder's answers looks the most accurate so far. – John Nov 04 '11 at 12:54