hossein ghodrati
hossein ghodrati

Reputation: 31

put data in tbody from fetch api

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

Upvotes: 0

Views: 221

Answers (1)

Igor Nowosad
Igor Nowosad

Reputation: 659

Change

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

to

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

Upvotes: 1

Related Questions