This example uses the HTML DOM to assign an "onkeypress" event to an input element.

Press a key inside the text field to set a blue background color.





document.getElementById("demo").onkeypress = function() {myFunction()};

function myFunction() {
    document.getElementById("demo").style.backgroundColor = "blue";
}


Back