Search code examples
htmleventsinputonkeypress

HTML Input onkeypress event is not working as I need


I've this input in a search modal used to find products by Id or Description in this system I'm working:

<input class="input-xlarge" type="text" onkeypress="productsDescription(this.value);">

And in my javascript I have this function

function productsDescription(val) {
    alert(val);
}

It happens that when I press a key inside this input, the alert function shows me the value as it was BEFORE the key was pressed. Is there a way to capture the valeu AFTER the key?


Solution

  • Use the onkeyup event instead the onkeypress happens BEFORE the value gets changed

    <input class="input-xlarge" type="text" onkeyup="productsDescription(this.value);">