I’m trying to correctly inject api’s data into a html table, with jquery.
What I’m missing, is the syntax to create recursively an id to a “tr” like that :
<tr id=...></tr>
in order to inject a row of data.
This is the current code :
$( document ).ready(function() {
$.getJSON('https://official-joke-api.appspot.com/random_ten', function(i) {
console.log(i);
let titre = i[0];
console.log(titre);
$.each(titre, function (index, element) {
let head = (`<th scope="col">${index}</th>`);
$('#head').append(head);
});
$.each(i, function (index, element){
let rangee = `<tr id="i"></tr>`;
$('#table-body').append(rangee);
$.each(element, function (key, value){
let row = (`<tr>
<td scope="col">${value}</td>
</tr>`)
$('#i').append(row);
});
});
});
});
The problem is at the line :
let rangee = `<tr id="i"></tr>`;
I don’t how to make the “i” detected as the number of the iteration…
Now, all my datas are in the first rank of my table.
I tried all sorts of weird syntax with variation of quotation marks, but nothings works for now…
Thank you for your help !
2
Answers
You can take help of
index
instead ofi
Ankit has provided the right answer.
Here is a tidy version of the same answer