Cambiar la página con código
Acá se juntan las tres cosas: HTML pone los elementos, CSS los viste, y JavaScript los modifica mientras la página está abierta.
Changing the page with code
Here the three come together: HTML places the elements, CSS dresses them, and JavaScript changes them while the page is open.
Para cambiar algo, primero hay que encontrarlo. Por eso servía el atributo
id que viste en HTML.
La instrucción es document.getElementById("nombre"). Literalmente: "del
documento, dame el elemento con este id".
Una vez que lo tienes, .textContent es su texto: puedes leerlo y cambiarlo.
To change something, first you have to find it. That is what the
id attribute from the HTML tutorial was for.
The instruction is document.getElementById("name"). Literally: "from the
document, give me the element with this id".
Once you have it, .textContent is its text: you can read it and change it.
También puedes cambiar el CSS desde JavaScript, con .style.
Un detalle: las propiedades con guion cambian de nombre. background-color en
CSS se escribe backgroundColor en JavaScript, porque el guion ahí significaría
una resta.
You can also change the CSS from JavaScript, using .style.
One detail: hyphenated properties change name. background-color in CSS becomes
backgroundColor in JavaScript, because a hyphen there would mean subtraction.
Acá está lo realmente potente: puedes crear HTML que no existía. Es como se arman las listas de productos, los mensajes de un chat o las tarjetas de este sitio.
Son tres pasos: crear el elemento, ponerle contenido, y agregarlo a la página.
Here is the truly powerful part: you can create HTML that did not exist. It is how product lists, chat messages and this site's cards are built.
Three steps: create the element, give it content, add it to the page.
Encontrar elementos con getElementById, cambiar su texto y su estilo, y crear
elementos nuevos desde datos.
Todo esto pasa al cargar la página. Lo que falta es que reaccione a lo que hace el usuario.
Finding elements with getElementById, changing their text and style, and
creating new elements from data.
All of this happens when the page loads. What is missing is reacting to what the user does.