[Solved] How do I use JavaScript to respond to a key press? [duplicate]


There are a lot of ways to do this, most people use jQuery but below is an example with raw JavaScript.

You can usually listen to either key down or key up. In most cases, you won’t want code to execute until the key is released (key up).

<!DOCTYPE html>
<html>
<head>
<script>
function makeUpperCase() {
    var x = document.getElementById("element-id");
    x.value = x.value.toUpperCase();
}
</script>
</head>
<body>

<p>When you enter a character in this field, it will be made uppercase on key up.</p>
Enter your name: <input type="text" id="element-id" onkeyup="makeUpperCase()">

</body>
</html>

solved How do I use JavaScript to respond to a key press? [duplicate]