Javascript Last character missing on OnKeyPress event

Wasim A. picture Wasim A. · Mar 17, 2011 · Viewed 9.3k times · Source
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'

Answer

Lixas picture Lixas · Mar 17, 2011

your event fires before letter is registered. you should use onkeyup event. It kicks-in after you release key