I
I
Ivan2019-08-13 21:00:05
JSON
Ivan, 2019-08-13 21:00:05

How to loop through external JSON and output data to DOM tree?

5d52f6f577241636262755.png

There is a JSON file which lies in a root. It is necessary that li is filled from "links" (content.json), and the block "right", displays the text from "content" (content.json) when clicking on li .

For example:
Click on the first li , text_1 is displayed in the right block (loaded from json)
Click on the third li , text_3 is displayed in the right block .

I only managed to fill in all li, but they are all filled with the value of the last key: header_link_5

https://codepen.io/stevegerr/pen/ZEzQrQj

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-08-13
@SteveGerr

const right = document.querySelector('.right');
const links = document.querySelectorAll("li");
const handleClick = (e) => right.textContent = data.content[data.links.indexOf(e.target.textContent)].news;


links.forEach((elem, i) => {
  elem.textContent = data.links[i];
  elem.addEventListener('click', handleClick);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question