Hello guys i’m a bit confused here.I have a question about how to loop this code so in the end result will be each data displayed stand-alone inside card ( each data have different id from the url in https://jsonplaceholder.typicode.com/posts ). Here is the code :
html
<div class="container">
<div class="card bg-warning">
<!-- put item.userId & item.id below this -->
<div class="card-header"></div>
<div class="card-body">
<!-- put item.title below this -->
<h5 class="card-title"></h5>
<!-- put item.body below this -->
<p class="card-text"></p>
</div>
</div>
</div>
js
$(function () {
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts",
success: function (result) {
$.each(result, function (index, item) {
var userId = item.userId;
var typeId = item.id;
var titleId = item.title;
var bodyId = item.body;
var $head = $(".card-header").html("user id: " + userId + " - " + "id: " + typeId);
var $title = $(".card-title").html(titleId);
var $text = $(".card-text").html(bodyId);
});
// console.log('success', result);
// console.log(result[0].body);
// console.log($(result).length);
}
});
});
4
Answers
You can use
.clone()
to clone your div which is there inside your container then using this you can add value inside cloned html and append it to your container.Demo Code :
You can also use append to add dynamic DOM inside your .each loop.
I have added working snippet for you.