Add one div within another using JQuery AJAX

0

I have a div within HTML a DIV element

<div class="row">

</div>

And in a JS file I have a JQUERY function

$.ajax({
        url: 'https://swapi.co/api/people/?format=json',
        type:'GET',
        dataType: 'JSON',
        success: function(per){
            for (var i = 0; i < per.results.length; i++) {
                var a = $("<div class='col-md-4'>sss</div>");
                $("div .row").after(a);
                console.log(per.results[i].name);
            }
        }
    });

I need for each element of the JSON to create a div and be included within the HTML div

    
asked by Ernesto Emmanuel Yah Lopez 14.10.2017 в 13:14
source

2 answers

2

Good test with an append.

$.ajax({
        url: 'https://swapi.co/api/people/?format=json',
        type:'GET',
        dataType: 'JSON',
        success: function(per){
            var a = "<div class='col-md-4'>sss</div>";
            for (var i = 0; i < per.results.length; i++) {
                $("div .row").append(a);
                console.log(per.results[i].name);
            }
        }
    });
    
answered by 14.10.2017 / 13:20
source
0

What you can do is create your div element as you already do, then append the value of your JSON and finally anéxalo to your row

$.ajax({
    url: 'https://swapi.co/api/people/?format=json',
    type:'GET',
    dataType: 'JSON',
    success: function(per){
        for (var i = 0; i < per.results.length; i++) {
            var a = $("<div class='col-md-4'></div>");
            a.append(per.results[i].name);
            $("div .row").append(a);
        }
    }
});
    
answered by 14.10.2017 в 15:22