put data in tbody from fetch api

177 Views Asked by At

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);




        });





    });





});

image of code when use the

1

There are 1 best solutions below

0
On

Change

document.querySelector("thead").insertAdjacentHTML('afterend', markup);

to

document.querySelector("tbody").insertAdjacentHTML('beforeend', markup);