javascript - keypress event not capturing target value in ES6 -
i've been refactoring small javascript project es6 , noticed keypress
event listener applied text input not including pressed character in target value event. behaviour can seen in fiddle:
http://jsbin.com/yayinalato/edit?js,console,output
$input = document.queryselector('.input') $input.addeventlistener('keypress', (e) => { console.log(e.target.value) })
the keyup
event works captures non-text input keypresses arrow keys etc undesirable this. why i'm confused why keypress
event not work under impression keyup had happen keypress.
how can value of target include key pressed event? explanation of how these events captured helpful.
e.target refers element triggered event, input field. if check e.target.value value of input field @ moment keypress event fired. happens before text entered in field.
what need use e.keycode check key pressed.
$input.addeventlistener('keypress', (e) => { console.log("keypressed " + e.charcode); })
also note (e)=>
works in browsers support es6, or have use babel convert es5
Comments
Post a Comment