6

Using jQuery, I would like to capture a keyboard event that is:

  • before the user lifts their finger from the key
  • after the characters from the keyboard event have registered in the input box.

To clarify, view this example. When keypress fires, the input value has not been updated yet.

[Edit]

Apparently I wasn't clear as to what I need.

The function must be called before the user lifts their finger up from the key, but after the key's character is placed in the input box. So the following do not work:

  • keydown: at the keypress event, the value in the text box has not been updated
  • keypress: at the keypress event, the value in the text box has not been updated
  • keyup: this is called when the user lifts their finger, which is too late.

4 Answers 4

13

You can use the input event, which works in recent versions of all major browsers:

var input = document.getElementById("your_input_id");
input.oninput = function() {
    alert(input.value);
};

Unfortunately, it doesn't work in IE <= 8. However, in those browsers you can use the propertychange event on the value property instead:

input.onpropertychange = function() {
    if (window.event.propertyName == "value") {
        alert(input.value);
    }
};

SO regular JavaScript answerer @Andy E has covered this in detail on his blog: https://web.archive.org/web/20140626060232/http://whattheheadsaid.com/2011/10/update-html5-oninput-event-plugin-for-jquery

5
  • Amazing, I have never seen this event before, but it works perfectly!
    – S W
    Apr 2, 2011 at 19:36
  • 1
    @franzlorenzon: Yeah, Andy's blog seems to be broken. I'll message him.
    – Tim Down
    Aug 1, 2013 at 17:35
  • What if change in input is done programmatically? Is there a way to catch this change? (example: input is read only, changed only through some javascript code - how to catch this change?)
    – FrenkyB
    Jul 22, 2017 at 16:30
  • Please update or remove that blog link, its redirecting to spammer website.
    – Maidul
    Jul 14, 2019 at 15:03
  • @Maidul: That's a shame. There were a couple of useful things on that site. I'll remove or replace the link.
    – Tim Down
    Jul 15, 2019 at 15:34
3

Use keyup event, an example on jsFiddle

$("textarea").keyup(function(e){
   alert($(this).val());
});

It happens after you lift the key. I don't know what you want to achieve, but you can store the state before lifting the key (on keydown or keypress) and restoring later if needed. You also can stop the output in the keyup event using e.preventDefault(), so even after the key is up it will not register the values in the area.

3

You could listen on keydown event and store the value in a variable. That variable would have the value as it was before the new input, and the new input would be included in the keyup event

UPDATE:

Ok, I misunderstood your requirements, but there isn't an event that would meet your needs. The only thing I can think of to simulate this behaviour is the following:

  • listen on keydown/keypress
  • get the value from the event object (get event.which, then convert it to actual value)
  • use a variable like I mentioned in the original advice and concatenate the new input to it

Here is a fiddle: http://jsfiddle.net/HpXuU/13/

This is obviously not a perfect solution, as it needs some (one might argue unnecessary) work to get done right. I would advise to rethink your needs, but if this behavior is absolutely what you need, I think this is a step in the right direction.

5
  • I need the new input value before the key is lifted.
    – S W
    Apr 1, 2011 at 23:38
  • So you need the code to be psychic and know the user is going to lift their finger the next millisecond and not the one after?
    – mVChr
    Apr 1, 2011 at 23:40
  • @SW Well, umm... isn't that exactly what my suggestion would do? Here is a fiddle: jsfiddle.net/HpXuU/10
    – Daniel
    Apr 1, 2011 at 23:44
  • keydown will give you the value before the key is lifted
    – lxalln
    Apr 1, 2011 at 23:46
  • The fiddle does not work if the user is not typing at the end of the text box.
    – S W
    Apr 2, 2011 at 0:26
2

You can use setTimeout:

$('input').keypress(function() {
    var that = this;
    setTimeout(function() {
        // read that.value
    }, 0);
});

Live demo: http://jsfiddle.net/HpXuU/8/

2
  • Unless I am mistaken, the timeout is not guaranteed to be called right at it's appointed time.
    – S W
    Apr 2, 2011 at 0:20
  • @SW The setTimeout mechanism indeed has a minimum delay which is cca. 10ms (depending on the browser). However, you can define a setZeroTimeout function (code is here) which will execute immediately after the keypress event handler returns. Apr 2, 2011 at 0:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.