Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFranklin Rony Cortez Barrera
3,065 PointsWhats wrong with my code
Hi I try to put in practice the learned in the video. I try the next code in the browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="" method="POST"> <label for="nombre">Escriba un Nombre</label> <input type="text" id="nombre" /> <label for="apellidos">Escriba sus apellidos</label> <input type="text" id="apellidos" /> <label for="edad">Escriba su edad</label> <input type="text" id="edad" /> <button type="submit" id="agregar">Enviar</button> </form> <table border="1" id="tabla"> <tr> <td>Nombre</td> <td>Apellidos</td> <td>Edad</td> </tr> </table> <script> let personas= new Array(); let agregar=document.getElementById("agregar"); agregar.addEventListener("click",function(e){ e.preventDefault(); let nombre=document.getElementById("nombre").value; let apellidos=document.getElementById("apellidos").value; let edad=document.getElementById("edad").value; let persona={"nombre":nombre,"apellidos":apellidos,"edad":edad}; personas.push(persona); //crear las filas necesarias por cada registro for(let i=0;i<personas.length;i++){ var tr=document.createElement("tr"); //Lllenar cada fila con los registros de cada elemento del arreglo for (var key in i) { let td=document.createElement("td"); let txt =document.createTextNode(i[key]); td.appendChild(txt); tr.appendChild(td); } } let tblBody= document.getElementsByTagName("tbody")[0].appendChild(tr); document.getElementById("tabla").appendChild(tblBody); }); </script> </body> </html>
The main idea is fill the fields in the form and then add in a table. But the issue its thats the cells dont create in the view
2 Answers
Steven Parker
231,236 PointsIt's hard to read the unformatted code, but I managed to spot a couple of issues:
- the array index was being used directly instead of the item at the index
- the table row was being moved behind the table body
Here are the relevant code lines, with corrections:
// for (var key in i) { <- "i" is just a number
for (var key in personas[i]) { // use object instead
//-----
// let txt = document.createTextNode(i[key]); <- same problem
let txt = document.createTextNode(personas[i][key]); // use object instead
//-----
// this assignment is not needed:
// let tblBody = document.getElementsByTagName("tbody")[0].appendChild(tr);
document.getElementsByTagName("tbody")[0].appendChild(tr);
//-----
// don't move the row after body
// document.getElementById("tabla").appendChild(tblBody);
Franklin Rony Cortez Barrera
3,065 PointsNice, work like a charm! Thank you so much. I learned a lot :)