Search code examples
javascriptdom-eventsonkeypress

Javascript Last character missing on OnKeyPress event


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'


Solution

  • var unicode=e.keyCode? e.keyCode : e.charCode;
    typing = document.getElementById('textbox').value + String.fromCharCode(unicode);