4

This is my first question on stack overflow, so I hope that I'm doing this correctly!

I'm working with pulling some data from an API, and then displaying that data in a table. So far my table is showing the HTML elements of <tr><td> + </tr></td>

Here is a photo to show my issue :

Photo of issue

Here is the code that I'm using :

const fetchUserz = async() => {
  const response = await fetch(`https://jsonplaceholder.typicode.com/users`);
  const data = await response.json();

  for (let i = 0; i < data.length; i++) {
    let table = document.getElementById(`myTable`);
    let row = `<tr>
 <td>${data[i].name}</td>
 <td>${data[i].email}</td>
 </tr>`;
    table.innerHTML += row;
    table.append(row);
  }
}

fetchUserz();

Here is the HTML :

<body class="text-white bg-secondary mb-3 bg-gradient" id="body">


 <main>
    <header id="head">
      <h2>API Project</h2>


  </header>
    <table>
      <thead>
        <tr>


 <th> Names of People </th>
    <th> Emails of People </th>
    </tr>
  </thead>

const fetchUserz = async() => {
  const response = await fetch(`https://jsonplaceholder.typicode.com/users`);
  const data = await response.json();
  // console.log(data)

for(let i = 0; i < data.length; i++){
  let table = document.getElementById(`myTable`);

 let row = `<tr>
  <td>${data[i].name}</td>
  <td>${data[i].email}</td>
  </tr>`;


  table.innerHTML += row;
  table.append(row);
}


}

fetchUserz();
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset = "UTF-8">
  <title>Native Awakenings</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.css" rel="stylesheet" crossorigin="anonymous">
  <link href="/css/stylesheet.css" rel="stylesheet">
</head>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="/index.html">Native Awakenings</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="/index.html">Home</a>
        </li>
        <li class="nav-item">
          <a href="about-me.html" class="nav-link">About Me</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Concious Creations
          </a>
          <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
            <li><a class="dropdown-item" href="blog-posts.html">Blog Posts</a></li>
            <li><a class="dropdown-item" href="offerings.html">Offerings</a></li>
            <li><a class="dropdown-item" href="podcasts.html">Podcasts</a></li>
            <li><a class="dropdown-item" href="yoga-videos.html">Yoga Videos</a></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item" href="https://gitlab.com/Gregg-Hendrix/how-i-became-a-software-engineer" target="_blank">How I Became A Software Engineer</a></li>
          </ul>
        </li>
      </ul>
      <span class="navbar-text">
        May Presence Be Our Purpose
      </span>
    </div>
  </div>
</nav>

<body class="text-white bg-secondary mb-3 bg-gradient" id="body">
  <main>
    <header id="head">
      <h2>API Project</h2>
    </header>
    <table>
      <thead>
        <tr>
        <th> Names of People </th>
        <th> Emails of People </th>
        </tr>
      </thead>

    <tbody id="myTable">

    </tbody>
  </main>
  <footer class="card-footer bg-dark bg-gradient navbar-dark text-light" id="footer"> If you want to support Native Awakenings, please do one kind act! Remember that you are unconditionally loved. My social media: Insta:<a href="https://www.instagram.com/greggyogi/" target="_blank"> @Greggyogi</a>, Email: gregg@gregghendrix.com, Facebook:<a href="https://www.facebook.com/gregg.hendrix.129" target="_blank"> Gregg Hendrix</a></footer>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-U1DAWAznBHeqEIlVSCgzq+c9gqGAJn5c/t99JyeKa9xxaYpSvHU5awsuZVVFIhvj" crossorigin="anonymous"></script>
  <script src="/js/weather.js"></script>
  </body>
  </html>

I've also added a snippet of EVERYTHING, any help would be massively appreciated :).

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 3
    You are calling `table.append(row);` right after doing `innerHTML+=row`. You are already adding the row to the table with the `+=` no need to do the append, which since you are passing it a string instead of an element is appending a textnode – Patrick Evans Sep 10 '21 at 17:43
  • 1
    I can confirm the previous comment. Remove `table.append(row);` and all will be good. This was easy to diagnose because you provided the snippet. Good job! – Rob Moll Sep 10 '21 at 17:46

3 Answers3

1

You immediate problem is using both innerHTML and append as covered in the comments

Let's go with innerHTML...

It's probably better to create your HTML string in one go rather than el.innerHTML += .... My gut feeling it that .innerHTML += will have pretty bad performance as each round will require an HTML parse and an HTML serialize.

const table = document.getElementById(`myTable`);
const rows = data.map(d=>`<tr>
    <td>${data[i].name}</td>
    <td>${data[i].email}</td>
  </tr>`);
table.innerHTML = rows.join("");
spender
  • 117,338
  • 33
  • 229
  • 351
  • Wow, thank you SO much! Removing table.append(row), worked! Thank you also @spender for going over and above, and helping my website have better performance. This was so valuable to me, and I feel deep gratitude for you all! Thank you! – Gregg_Hendrix Sep 10 '21 at 18:14
1

You can create your HTML string as below.

   
const fetchUserz = async() => {
  
const data = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "Shanna@melissa.tv",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
  {
    "id": 3,
    "name": "Clementine Bauch",
    "username": "Samantha",
    "email": "Nathan@yesenia.net",
    "address": {
      "street": "Douglas Extension",
      "suite": "Suite 847",
      "city": "McKenziehaven",
      "zipcode": "59590-4157",
      "geo": {
        "lat": "-68.6102",
        "lng": "-47.0653"
      }
    },
    "phone": "1-463-123-4447",
    "website": "ramiro.info",
    "company": {
      "name": "Romaguera-Jacobson",
      "catchPhrase": "Face to face bifurcated interface",
      "bs": "e-enable strategic applications"
    }
  },
  {
    "id": 4,
    "name": "Patricia Lebsack",
    "username": "Karianne",
    "email": "Julianne.OConner@kory.org",
    "address": {
      "street": "Hoeger Mall",
      "suite": "Apt. 692",
      "city": "South Elvis",
      "zipcode": "53919-4257",
      "geo": {
        "lat": "29.4572",
        "lng": "-164.2990"
      }
    },
    "phone": "493-170-9623 x156",
    "website": "kale.biz",
    "company": {
      "name": "Robel-Corkery",
      "catchPhrase": "Multi-tiered zero tolerance productivity",
      "bs": "transition cutting-edge web services"
    }
  },
  {
    "id": 5,
    "name": "Chelsey Dietrich",
    "username": "Kamren",
    "email": "Lucio_Hettinger@annie.ca",
    "address": {
      "street": "Skiles Walks",
      "suite": "Suite 351",
      "city": "Roscoeview",
      "zipcode": "33263",
      "geo": {
        "lat": "-31.8129",
        "lng": "62.5342"
      }
    },
    "phone": "(254)954-1289",
    "website": "demarco.info",
    "company": {
      "name": "Keebler LLC",
      "catchPhrase": "User-centric fault-tolerant solution",
      "bs": "revolutionize end-to-end systems"
    }
  },
  {
    "id": 6,
    "name": "Mrs. Dennis Schulist",
    "username": "Leopoldo_Corkery",
    "email": "Karley_Dach@jasper.info",
    "address": {
      "street": "Norberto Crossing",
      "suite": "Apt. 950",
      "city": "South Christy",
      "zipcode": "23505-1337",
      "geo": {
        "lat": "-71.4197",
        "lng": "71.7478"
      }
    },
    "phone": "1-477-935-8478 x6430",
    "website": "ola.org",
    "company": {
      "name": "Considine-Lockman",
      "catchPhrase": "Synchronised bottom-line interface",
      "bs": "e-enable innovative applications"
    }
  },
  {
    "id": 7,
    "name": "Kurtis Weissnat",
    "username": "Elwyn.Skiles",
    "email": "Telly.Hoeger@billy.biz",
    "address": {
      "street": "Rex Trail",
      "suite": "Suite 280",
      "city": "Howemouth",
      "zipcode": "58804-1099",
      "geo": {
        "lat": "24.8918",
        "lng": "21.8984"
      }
    },
    "phone": "210.067.6132",
    "website": "elvis.io",
    "company": {
      "name": "Johns Group",
      "catchPhrase": "Configurable multimedia task-force",
      "bs": "generate enterprise e-tailers"
    }
  },
  {
    "id": 8,
    "name": "Nicholas Runolfsdottir V",
    "username": "Maxime_Nienow",
    "email": "Sherwood@rosamond.me",
    "address": {
      "street": "Ellsworth Summit",
      "suite": "Suite 729",
      "city": "Aliyaview",
      "zipcode": "45169",
      "geo": {
        "lat": "-14.3990",
        "lng": "-120.7677"
      }
    },
    "phone": "586.493.6943 x140",
    "website": "jacynthe.com",
    "company": {
      "name": "Abernathy Group",
      "catchPhrase": "Implemented secondary concept",
      "bs": "e-enable extensible e-tailers"
    }
  },
  {
    "id": 9,
    "name": "Glenna Reichert",
    "username": "Delphine",
    "email": "Chaim_McDermott@dana.io",
    "address": {
      "street": "Dayna Park",
      "suite": "Suite 449",
      "city": "Bartholomebury",
      "zipcode": "76495-3109",
      "geo": {
        "lat": "24.6463",
        "lng": "-168.8889"
      }
    },
    "phone": "(775)976-6794 x41206",
    "website": "conrad.com",
    "company": {
      "name": "Yost and Sons",
      "catchPhrase": "Switchable contextually-based project",
      "bs": "aggregate real-time technologies"
    }
  },
  {
    "id": 10,
    "name": "Clementina DuBuque",
    "username": "Moriah.Stanton",
    "email": "Rey.Padberg@karina.biz",
    "address": {
      "street": "Kattie Turnpike",
      "suite": "Suite 198",
      "city": "Lebsackbury",
      "zipcode": "31428-2261",
      "geo": {
        "lat": "-38.2386",
        "lng": "57.2232"
      }
    },
    "phone": "024-648-3804",
    "website": "ambrose.net",
    "company": {
      "name": "Hoeger LLC",
      "catchPhrase": "Centralized empowering task-force",
      "bs": "target end-to-end models"
    }
  }
]

  let table = document.getElementById(`myTable`);  
      
       for(let i = 0; i < data.length; i++){
          let row = '';
             row +='<tr>';
             row +='<td>'+data[i].name+'</td>';
             row +='<td>'+data[i].email+'</td>';
             row +='</tr>';
          table.innerHTML += row;
       }
}

fetchUserz();
  
    <table>
      <thead>
        <tr>
       <th> Names of People </th>
       <th> Emails of People </th>
    </tr>
  </thead>
   <tbody id="myTable">

    </tbody>
      <tbody id="myTable2">

    </tbody>
  </table>
Mahi
  • 3,748
  • 4
  • 35
  • 70
1
    const fetchUserz = async() => {
  const response = await fetch(`https://jsonplaceholder.typicode.com/users`);
  const data = await response.json();
  // console.log(data)

for(let i = 0; i < data.length; i++){
  let table = document.getElementById(`myTable`);

 let row = `<tr>
  <td>${data[i].name}</td>
  <td>${data[i].email}</td>
  </tr>`;


  table.innerHTML += row;
  
}


}

fetchUserz();
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 10 '21 at 19:00