Ad

How To Populate HTML With Data From Jquery (multiple Nested Div)

I want to Populate elements inside class info-wrapper using JQuery. Data that has to populate are inside data variable in JQuery. I can't use $('#id').append("div") like these syntax because i want to nest multiple div inside each other and popluate .card-wrapper and its child elements for every data item.

Jquery


    $.ajax({
        method: 'GET',
        url: 'getUser/admin',
        dataType: 'json',
        success: function (data) {
            console.log(typeof(data));
            data = Object.entries(data);
            for(var i = 0;i<data.length;i++)
            {

            }
        },
        error: function () {

        }
    })

Blade Template

@extends('layouts.app')

@section('admin-content')
<div class="container">
                    <div class="row" id="inject">
                        <div class="col-md-6  col-sm-12">
                            <div class="card-wrapper">
                                <div class="info-wrapper">
                                    <h2 id="name"></h2>
                                    <h6 id="email"></h6>
                                    <hr/>
                                    <h6 id="department"></h6>
                                    <h6 is="sem"></h6>
                                </div>
                                <div class="button-wrapper">
                                    <button class="btn btn-danger" type="submit">Delete Account</button>
                                    <button class="btn btn-secondary" type="submit">Block Account</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
@endsection
Ad

Answer

Because info-wrapper is just a single element, you can instead populate elements inside the div with inject id, you can make changes to your js to generate elements dynamically, I hope this can help.

    $.ajax({
    method: 'GET',
    url: 'getUser/admin',
    dataType: 'json',
    success: function (data) {
        console.log(typeof(data));
        data = Object.entries(data);

        let html = '';
        for(var i = 0;i<data.length;i++)
        {
          html += `
            <div class="card-wrapper">
              <div class="info-wrapper">
                  <h2 id="name">${data[i].name}</h2>
                  <h6 id="email">${data[i].email}</h6>
                  <hr/>
                  <h6 id="department">${data[i].department}</h6>
                  <h6 is="sem"><${data[i].sem}/h6>
              </div>
              <div class="button-wrapper">
                  <button class="btn btn-danger" type="submit">Delete Account</button>
                  <button class="btn btn-secondary" type="submit">Block Account</button>
              </div>
          </div>
          `;
        }

        html = `<div class="col-md-6  col-sm-12">${html}</div>`

        $('#inject').html(html);
    },
    error: function () {

    }
})
Ad
source: stackoverflow.com
Ad