function h(x)
{
alert(x);
}
<input onkeypress=h(this.value) type=text>
When I press 'a' alert empty
When I press 'b' after 'a' =>ab alert only 'a' and I want 'ab'
When I type 'abcd' it alert 'abc' only and I want 'abcd'
keydown – fires when any key is pressed down, fires first, and always before the browser processes the key (e.g. inserting text, moving focus, etc). keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key.
The simple solution: just use the keyup event (and not the keydown event). This will give you the latest value after the user typed the latest character. That resolves the issue.
If onkeyPress returns false, the key-down event is cancelled.
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .
Javascript:
function h(x)
{
alert(x);
}
HTML Code:
<input onkeyup=h(this.value) type=text>
your event fires before letter is registered. you should use onkeyup event. It kicks-in after you release key
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With