Using "onkeydown" together with the "onkeyup" event:
Press and hold down a key inside the text field to set a red background color.
Release the key to set a green background color.
<!DOCTYPE html> <html> <body> <input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()"> <script> function keydownFunction() {/* w ww . j a v a2s . co m*/ document.getElementById("demo").style.backgroundColor = "red"; } function keyupFunction() { document.getElementById("demo").style.backgroundColor = "green"; } </script> </body> </html>