Search code examples
javascriptprototypejsdom-events

Prototype: how to get new input value after some key pressed?


I would like to check an email after customer pressed any key. This is my code:

document.observe("dom:loaded", function() {
   $('billing:email').observe('keypress', function(event){
        console.log(event.element().value);
   });
});

If I press "D" log will print "".
If I press "S" after "D" was pressed - log will print "D".

How to get actual current input value?


Solution

  • Try the input event instead. The classic key/up/down/press events are can be murky when trying to also read input changes. Seems that keypress fires before the character prints. But you'll also miss other events such as users backspacing over text. Quirks mode has the gory details.