I’m new to jQuery and I have got stuck on an each loop.
I’m trying to get the values from a variable with objects and when I print it using alert(), I get all the results.
But when I’m trying to print it to the HTML I only get the last result printed to the HTML instead of 3, in this case.
$.each(playlist, function(index, item) {
alert(item.title);
$('.playlist1').text(item.title);
});
Actually, you’re not stuck on the
.each()loop, you’re stuck on.text()..text()replaces the current text with new text, so you’ll only ever see the last one in this case.Maybe use
.append().You could also use
.text()in this fashion:Assuming
.playlist1is aulorol, you can append to it like this: