how to display data from ajax in html tag like p or div Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of how to display data from ajax in html tag like p or div without wasting too much if your time.

The question is published on by Tutorial Guruji team.

        url:'{{ route("dashboard.diklat_name") }}',
        data:{dari:dari, sampai:sampai},
        type: 'get',
        dataType: 'json',
        success:function(response) {
            var len = 0;
            len = response['data'].length;
            
            if (len > 0) {
                for (var index = 0; index < len; index++) {
                    document.getElementById('diklat_name').innerHTML = response['data'][index].name;
                    console.log(response['data'][index].name);
                }
            }
        }

this code give me 1 display. but in log it’s return as many data that i get.

for example i have 50 data and i want to display it in

thanks

Answer

Try this.

    url:'{{ route("dashboard.diklat_name") }}',
        data:{dari:dari, sampai:sampai},
        type: 'get',
        dataType: 'json',
        success:function(response) {
            var len = 0;
            len = response['data'].length;
            
            if (len > 0) {
                let data = '';
                response['data'].forEach((item) => {
                   data += "<p>" + item.name + "</p>";
                   console.log(item.name);
                )}
                document.getElementById('diklat_name').innerHTML = data;
            }
        }
We are here to answer your question about how to display data from ajax in html tag like p or div - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji