Key event handler
There are several key events. One of them is onkeypress
. onkeypress
gets triggered, well, you may have guessed this, whenever a key gets pressed. Pressing means here when the button is pressed and released. If you want an event to happen as soon as the button is pressed (so before releasing), you can use the onkeydown
event. If you want the event to happen on release, you can use the onkeyup
event.
There are many things we can do with key events. For example, we can restrict what characters can be entered in an input box. Every time a key gets pressed, we can check the character and decide whether it gets to stay.
We can get the key that triggered the event using:
event.key;
The following HTML code has two input boxes. Can you see what is happening here?
<!doctype html>
<html>
<body>
<body>
<div id="wrapper">JavaScript is fun!</div>
<input type="text" name="myNum1...