Reputation: 31
Hello i want to put data from api in table's tbody but when i use below code. It is created one tbody tag for each data . I want all the data to be in one tbody tag. how can i do ?
fetch(get_data)
.then(function (response) {
response.text().then(function (responseText) {
var obj = JSON.parse(responseText);
obj.forEach(user => {
const markup = `
<td class="customer" id="company${user.id}">${user.company}</td>
<td class="customer" id="first_name${user.id}">${user.first_name}</td>
<td class="customer" id="last_name${user.id}">${user.last_name}</td>
`;
document.querySelector("thead").insertAdjacentHTML('afterend', markup);
});
});
});
Upvotes: 0
Views: 221
Reputation: 659
Change
document.querySelector("thead").insertAdjacentHTML('afterend', markup);
to
document.querySelector("tbody").insertAdjacentHTML('beforeend', markup);
Upvotes: 1