JavaScript onkeypress Event Example

Updated on     Kisan Patel

If we want to execute a javascript function when key is pressed from the keyboard on the html element, we need to use javascript onkeypress event.

<!-- HTML -->
Name: <input type="text" id="txtName" name="txtName" onkeypress="TestFunction()" >

<!-- Javascript -->
<script type="text/javascript">
function TestFunction() {
   alert("You pressed a key inside the input field");
}
</script>

In the above code snippet, we have specified onkeypress event on the textbox that executes TestFunction() function. In this function, we are display alert box.

See the Pen waZKra by Kisan (@pka246) on CodePen.


JavaScript

Leave a Reply