Skip to content
Advertisement

JavaScript print info of an array inside an array of object in a html section

I have this array of object that contains info string number array

I’m trying to print all the info of touristActivities inside an html <section>

I want to print first the information of stepOne in a section tag then the information of stepTwo in another section tag

touristActivities array can contain more the these two steps

my code below i can only print the info without the section

How can i print the info of each steps inside a different section for each step

example:

JavaScript

JavaScript
JavaScript

Advertisement

Answer

The thing is your functions directly modify the HTML. It is tightly coupled. So you lose quite a bit of control over the output. With your code the sequence of these function calls is important because the output will depend on the calls. E.g. first description() must be followed by time(). Now you want to wrap all this, but you have already written it to the DOM.

The sections span the content you want to generate from each step. Therefore, they have to wrap everything inside the for-loop there.

Instead of directly modifying the DOM, write the output of your functions to an array buffer. After the loop you will attach the whole content that you have created to the section element.

So you return the HTML from your functions, store it into the array buffer and once you looped over the section, you can combine/add the content with buffer.join('') and close it.

This way, you split the generators from the assembly code.

Here is a working example:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement