0

So I have an array called "comments"

comments: ["Comment1","Comment2","Comment3"]

Now I want to display every item as a new line in HTML

Other stuff from my object I display like this:

<p><%= singlegif.get("title") %></p>

But obviously putting "comments" in the above code displays them all next to eachother.. Now how do I display one/line?

j08691
  • 204,283
  • 31
  • 260
  • 272
vlovystack
  • 703
  • 3
  • 11
  • 20

1 Answers1

0

Javascript array prototype has a function forEach(callback(element, index, array)) which executes the provided callback once for each element of the array with an assigned value. It is not invoked for indexes which have been deleted or which have been initialized to undefined.

var commentsContainer = document.getElementById("demo");
comments: ["Comment1","Comment2","Comment3"];
comments.forEach(function(element, index){
   // create paragraph for each comment
   var elementForComment = document.createElement('p');

   // set text of paragraph
   elementForComment.innerText = element;

   // add paragraph into comments holder.
   commentsContainer.appendChild(elementForComment);
});
Epsil0neR
  • 1,676
  • 16
  • 24