3

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.

gdoron
  • 147,333
  • 58
  • 291
  • 367

6 Answers6

4

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.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 3
    Yes 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
0

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);
})();
Phil LaNasa
  • 2,907
  • 1
  • 25
  • 16
0

You can bind the keyup event.

$("textarea").bind("keyup", function() {
    // Fires after the user has typed
});

See: http://jsfiddle.net/AWUbu/1/

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
0
var obj = document.getElementById("field");
var func= function(){
    alert('hi');
}

obj.onkeypress= func;
obj.onclick = func;
obj.on... = func;
Daggeto
  • 943
  • 1
  • 8
  • 17
0

Aside from the bind method outlined in another answer, you can also do this:

$("#txtTest").keyup(function(){
    alert($(this).val());
});
James Johnson
  • 45,496
  • 8
  • 73
  • 110
0

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'); } );
John
  • 459
  • 2
  • 6
  • 2
    What about the mouse paste from clipboard case? – Darin Dimitrov Nov 02 '11 at 14:27
  • @DarinDimitrov As an expert, do you have a better solution? – gdoron Nov 03 '11 at 06:55
  • @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