Home > Mobile >  Please explain this MDN example
Please explain this MDN example

Time:10-30

I'm learning javascript through MDN documentation. They have an example on the function topic which apparently I'm unable to understand.

const textBox = document.querySelector("#textBox");

const output = document.querySelector("#output");

textBox.addEventListener('keydown', (event) => output.textContent = `You pressed "${event.key}".`);


I've read a few documentation onkey down function and I assume it is supposed to return some value for every character entered (correct me if I'm wrong); but in the example on MDN, it only returns enter when I press enter key and backspace when I press backspace (while input box has no other characters). I'm reading this example on a mobile device btw.

CodePudding user response:

Seems to be working, as long as you add the proper html.

const textBox = document.querySelector("#textBox");

const output = document.querySelector("#output");

textBox.addEventListener('keydown', (event) => output.textContent = `You pressed "${event.key}".`);
<input id="textBox">
<div id="output"></div>

  • Related