Using the Event
object, you can get
the keys pressed. Let's see how it's done.
Let's have an input:
<input>
Get a reference to it into a variable:
let elem = document.querySelector('input');
Let's hang an event on our input that fires on each keystroke:
elem.addEventListener('keypress', function(event) {
});
The key
property of the object
with the event will contain the
pressed character:
elem.addEventListener('keypress', function(event) {
console.log(event.key);
});
And the code
property will contain
the code of the pressed key:
elem.addEventListener('keypress', function(event) {
console.log(event.code);
});
Make an input that will display the values of the entered keys and their codes on input.
Press various keys in the input and see what their values and codes are.
Determine what code the Enter
key will have.
Determine what code the BackSpace
key will have.
Given a paragraph and an input. Text is
entered into it and the key Enter
is pressed. Make it so that at this moment
the entered text falls into the paragraph
under the input, and the contents of the
input are cleared.