views:

86

answers:

3
$("#epFilter").keypress(function(){
        query = $("#epFilter").val();
        alert(query);
});

In this code, whenever I type in my text box, it always alert one character less. It doesn't catch the last character. Why?

+1  A: 

Use the keyup event instead.

$("#epFilter").keyup(function(){
        query = $("#epFilter").val();
        alert(query);
});
Matt
+2  A: 

keypress event is triggered when user presses a key, but before the character is inserted. Use keyup event instead.

pawel
A: 

I wonder if that is a "normal" behavior. Shouldn't keypress be similar to keydown+keyup (except for the codes and keys)?

I had the same problem. I have an input-text field and I want to calculate a total on keypress. However, it does not change the total until I input again another character (this means when I entered the first digit it didn't update). I used "keyup" and was "fixed", but I think it could be better if I use keypress as it doesn't fire when I press other keys (like Ctrl, Shift, arrows, etc).

According to the documentation Keypress should work as I expected but for some reason it didn't.

This is not exactly an answer (so don't vote for it). Just my contribution.

lepe