Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Mixing and Matching Arrays and Objects

Whats 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
Steven Parker
216,083 Points

It'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);
//      don't move the row after body
//      document.getElementById("tabla").appendChild(tblBody);

Nice, work like a charm! Thank you so much. I learned a lot :)