Botones y formularios
Hasta ahora tu código corría solo al abrir la página. Acá empieza a responderle a la gente.
Buttons and forms
So far your code ran once when the page opened. Here it starts answering to people.
Un evento es algo que pasa: un clic, una tecla, mover el dedo. Para
reaccionar se usa addEventListener, que se lee "agrega un escuchador".
Recibe dos cosas: qué escuchar y qué hacer cuando ocurra. Esa segunda parte es una función — las mismas del módulo anterior.
An event is something that happens: a click, a key, a finger moving. To
react you use addEventListener — "add a listener".
It takes two things: what to listen for and what to do when it happens. That second part is a function — the same ones from the earlier module.
Un campo de texto es <input>. Para saber qué escribieron se usa
.value.
Acuérdate de esto: lo que sale de un input siempre es texto,
aunque escriban un número. Es exactamente el problema del "5" + "3" que viste en
variables.
A text field is an <input>. To know what was typed, use
.value.
Remember this: whatever comes out of an input is always text,
even if they typed a number. It is exactly the "5" + "3" problem from the
variables tutorial.
Acá está todo junto: un input, una lista de objetos que crece, y la página que se redibuja sola. Es, en pequeño, cómo funciona cualquier aplicación web.
Here it all comes together: an input, a growing list of objects, and the page redrawing itself. It is, in miniature, how any web application works.
Escuchar eventos con addEventListener, leer un input con
.value, validar antes de usar, y el patrón de cambiar datos y redibujar.
Tu lista funciona… hasta que se recarga la página y se pierde todo. Eso lo arreglamos en el siguiente.
Listening with addEventListener, reading an input with
.value, validating before using, and the change-data-then-redraw pattern.
Your list works… until the page reloads and everything is gone. We fix that next.