103

I got:

$(someTextInputField).keypress(function() {
  alert($(this).val());
});

Now the alert always returns the value BEFORE the keypress (e.g. the field is empty, I type 'a' and the alert gives me ''. Then I type 'b' and the alert gives me 'a'...). But I want the value AFTER the keypress - how can I do that?

Background: I'd like to enable a button as soon as the text field contains at least one character. So I run this test on every keypress event, but using the returned val() the result is always one step behind. Using the change() event is not an option for me because then the button is disabled until you leave the text box. If there's a better way to do that, I'm glad to hear it!

1
  • Should the button become disabled again if the text is deleted? If so, you'll probably have to stick with keypress.
    – Brilliand
    Jan 27, 2014 at 19:00

5 Answers 5

152

Change keypress to keyup:

$(someTextInputField).on("keyup", function() {
  alert($(this).val());
});

keypress is fired when the key is pressed down, keyup is fired when the key is released.

3
  • @Brilliand You're right. I made a workaround for this, it's posted below. Jan 26, 2014 at 4:08
  • 7
    how who you accomplisch this on iPhone/Android devices? They don't support the keyup function. Aug 27, 2014 at 14:10
  • 4
    definition of keypress is not exactly correct. that's the definition of keydown. see quirksmode.org/dom/events/keys.html
    – challet
    Oct 20, 2014 at 22:10
64

Surprised that no one mentioned the js "input" event:

$(someTextInputField).on('input', function() {
  alert($(this).val());
});

Recommended.

https://developer.mozilla.org/en-US/docs/Web/Events/input

4
  • 9
    Wow, this is the answer to several unanswered/badly answered SO questions. Nov 2, 2015 at 19:13
  • 4
    This is also great when you need to ignore pressing arrow keys, shift etc. in input field.
    – hovado
    Nov 19, 2015 at 14:02
  • 2
    caniuse.com/#search=input Relatively good browser support. Much better answer than listening to every keyup.
    – James Haug
    Aug 2, 2016 at 3:27
  • 1
    It saves my day! Thank you. I used the following code: $('.subject_mark').on("input", function () { var $th = $(this); $th.val($th.val().replace(/[^0-9]/g, function (str) { return ''; }));
    – arshovon
    Jan 5, 2017 at 11:50
9

instead of keypress, use keyup.

5

Alternatively, you can use the keydown event with a timeout of 0.

That way, the changes will be applied instantly, instead of being applied when the user stops holding the key.

$(someTextInputField).on("keydown", function() {
  setTimeout(function($elem){
    alert($elem.val());
  }, 0, $(this));
});
2
  • This will still behave strangely in the case of holding down Backspace... perhaps keypress with a timeout of 0?
    – Brilliand
    Jan 27, 2014 at 16:14
  • Ah, never mind, the OP doesn't specify what should happen if the text field is cleared.
    – Brilliand
    Jan 27, 2014 at 18:59
5

Try something like this:

$('#someField').keypress(function() {
  setTimeout(function() {
    if ($('#someField').val().length > 0)
      $('#theButton').attr('disabled', false);
  }, 1);
});

That simply introduces a timeout so that after the "keypress" event loop completes, your code will run almost immediately thereafter. Such a short timer interval (even if rounded up by the browser) will not be noticeable.

edit — or you could use "keyup" like everybody else says, though its semantics are different.

2
  • It's not a matter of benig smart. The keyup event doesn't provide the same result. You get distinct events for shift, ctrl,etc with keyup - if you want to get the actual character being typed (such as an ampersand) rather than a series of key events that you have to check the state of shift or ctrl keys, keypress is the way to go.
    – Ripside
    Jun 5, 2015 at 16:42
  • 1
    Right; that's what I meant by "it's semantics are different".
    – Pointy
    May 6, 2016 at 12:53

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.