I want to generate row and column repeatably with data by jquery each function. Please help.
Required HTML Structure and jquery attempt:
var data = [{
"name": "Alice",
"surname": "Smith",
"rollnum": "00002"
},
{
"name": "Bob",
"surname": "Walter",
"rollnum": "00004"
}
];
load();
function load() {
var name = document.querySelector(".name");
var surname = document.querySelector(".surname");
$.each(data, function(index, value) {
var $name = value.name;
var $surname = value.surname;
var $rollnum = value.rollnum;
console.log($name);
console.log($surname);
console.log($name);
$(".name").text($name);
$(".surname").text($surname);
$(".rollnum").text($rollnum);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div class="eventsBlock">
<div class="row">
<div class="col-md-4 col-sm-4">
<span class="name">name</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="surname">surname</span>
<span class="rollnum">rollnum</span>
</div>
<div class="col-md-4 col-sm-4">
<span class="locaton">locaton</span>
</div>
</div>
</div>
2
Answers
You need to use
.append()
for this and need to change HTML structure a bit nearsurname
androllnumber
(put them in separate div as per my suggestion)It would be much better if you use table HTML like this:
This works if you’re using jQuery.