Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Andrei Duhanes
Andrei Duhanes
14,155 Points

A Javascript code for creating paragraphs not working well.

Hey guys,

Here is a quick one for you,

I need each item of the array to be posted when clicked individually, not all at once. Now, I understand why all are posted at the same time, but I can't figure out how to modify the code in order to make it work the way I want it.

I made it work using different variables and so on, but I want to make it work using arrays and the for loop, thanks in advance guys, and gals ^_^.

buttonNewPar.addEventListener("click", function() {

const paragraphMod = document.getElementsByClassName("blog-post")
                                       [0].querySelectorAll("p")[1];

const parag = [
"This is one Paragraph",
"This is the second Paragraph",
"This is the third Paragraph"      
];

for (let i=0; i<parag.length; i++)

{
   const parCreate = document.createElement("p");
   const parApend = parCreate.appendChild(document.createTextNode(parag[i]));
   paragraphMod.appendChild(parApend);
   paragraphMod.appendChild(document.createElement("br"));
   console.log(i);
 }
 });