Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);
 }
 });