TIMEX
TIMEX

Reputation: 271724

Jquery Keypress is not working for my input box

$("#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?

Upvotes: 1

Views: 10036

Answers (4)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

Slightly improving Matt's answer (using this inside event handler):

$("#epFilter").keyup(function(){
    query = $(this).val();
    alert(query);
});

Upvotes: 1

lepe
lepe

Reputation: 25200

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.

Upvotes: 0

pawel
pawel

Reputation: 36965

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

Upvotes: 4

Matt
Matt

Reputation: 75317

Use the keyup event instead.

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

Upvotes: 5

Related Questions