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.

2
  • Does the 'onchange' event not work for your textbox? Commented Nov 2, 2011 at 13:56
  • possible duplicate of <input> text change events Commented Nov 2, 2011 at 14:07

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

4 Comments

Yes there is: the HTML5 input event. stackoverflow.com/questions/5519936/jquery-keyboard-events/…. Works in everything except IE < 9, in which you can use the propertychange event instead.
@TimDown: You should have posted that as an answer. :-) I knew I was forgetting something. But unfortunately, various browsers have various bugs 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.
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.
@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.
0

You can bind the keyup event.

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

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

1 Comment

That ignores mouse events and keyrepeat.
0

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

$("#txtTest").keyup(function(){
    alert($(this).val());
});

Comments

0
var obj = document.getElementById("field");
var func= function(){
    alert('hi');
}

obj.onkeypress= func;
obj.onclick = func;
obj.on... = func;

1 Comment

He said he wants it proactively, not on blur. change isn't fired until the user exits the field.
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'); } );

5 Comments

What about the mouse paste from clipboard case?
@DarinDimitrov As an expert, do you have a better solution?
@gdoron, I am not an expert. T.J. Crowder's answer looks the best so far.
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
Also, someone could paste something from the browsers toolbar ( Edit > Paste ) so T.J. Crowder's answers looks the most accurate so far.
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);
})();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.