I'm starting to learn JavaScript, and i am having some difficulties for displaying the whole data i got from Json PlaceHolder fake API on my html table. Using the code below i just manage to display the last element from the API itself, and not all elements as i wanted.
Can someone help me figure it out how should i actually do?
There's the code:
fetch('https://jsonplaceholder.typicode.com/posts')
.then((response) => response.json())
.then((json) => json.forEach(tableElements)
)
function tableElements (element, index, arr){
arr[index] = document.querySelector('#posts-table tbody').innerHTML =
`<td>${element.userId}</td>
<td>${element.id}</td>
<td>${element.title}</td>
<td>${element.body}</td>`
}
x.innerHTML = y;multiple times, you keep overwriting the content of x. Only the last y will be visible. You need to wrap your table cells in<tr>and</tr>and use the+=operator instead, which adds what's on the right to what's on the left of it.